// Like any other piece of software (and information generally), // FastFileAlloc comes with NO WARRANTY. // FastFileAlloc.cs - generate empty files of any size quickly // Version 0.04 // Written 2005-03-20 // by Toby Betts // FastFileAlloc creates files of a specific size very quickly. Files // created with FastFileAlloc do not contain meaningful information. // Do not attempt to read a file created with FastFileAlloc in any // meaningful way without first writing valid data in it first. // FastFileAlloc accepts a filename and a size as command line // arguments. Both arguments are required. // Example: to create a file called "test.file" exactly 12,345,678 // bytes long, run the following at a command prompt: // FastFileAlloc.exe test.file 12345678 // You may also use case-insensitive abbreviations in your filesizes: // 1G = 1g = 1 gigabyte // 1M = 1m = 1 megabyte // 1K = 1k = 1 kilobyte // 1B = 1b = 1 byte (this is the default size) // Example: to create a file called "sixmegs.txt" exactly 6 megabytes // long, run either of the following at a command prompt: // FastFileAlloc.exe sixmegs.txt 6291456 // or // FastFileAlloc.exe sixmegs.txt 6m using System; using System.IO; using System.Runtime.InteropServices; namespace FastFileAlloc { class FastFileAlloc1 { const string FATAL = "FastFileAlloc: fatal:"; [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetDiskFreeSpaceEx( string lpDirectoryName, ref long lpFreeBytesAvailable, ref long lpTotalNumberOfBytes, ref long lpTotalNumberOfFreeBytes ); [STAThread] static void Main(string[] args) { string fn = ""; string filesize = ""; byte i; char ch = 'Z'; Int64 count; string tmp =""; long lpFreeBytesAvailable = 0; long lpTotalNumberOfBytes = 0; long lpTotalNumberOfFreeBytes = 0; FileStream fs = null; if (2 != args.Length) { FastFileAlloc1.usage(); return; } try { fn = Path.GetFullPath(args[0]); } catch (Exception e) { Console.Error.WriteLine("{0} bad filename",FATAL); return; } try { int gdfsx = GetDiskFreeSpaceEx( Path.GetPathRoot(fn).ToString(), ref lpFreeBytesAvailable, ref lpTotalNumberOfBytes, ref lpTotalNumberOfFreeBytes ); } catch (Exception e) { Console.Error.WriteLine("{0} GetDiskFreeSpaceEx error",FATAL); return; } if (File.Exists(fn)) { if (FileAttributes.ReadOnly == (File.GetAttributes(fn) & FileAttributes.ReadOnly)) { Console.Error.WriteLine("{0} file exists and is read-only",FATAL); return; } } try { filesize = args[1].ToLower(); if (0 == filesize.Length) throw new ArgumentOutOfRangeException(); count = 0; for (i = 0; i < filesize.Length; ++i) { ch = filesize[i]; if ((!Char.IsDigit(ch)) && (0 == i)) { throw new ArgumentOutOfRangeException(); } if (Char.IsDigit(ch)) { count *= 10; count += (ch - 48); continue; } if ('g' == ch) { count *= 1024; ch = 'm'; } if ('m' == ch) { count *= 1024; ch = 'k'; } if ('k' == ch) { count *= 1024; ch = 'b'; } if ('b' == ch) { break; } if (',' == ch) { continue; } throw new ArgumentOutOfRangeException(); } //count = Convert.ToInt64(args[1],10); if (0 > count) throw new ArgumentOutOfRangeException(); if (lpFreeBytesAvailable < count) throw new ArgumentOutOfRangeException(); } catch (Exception e) { Console.WriteLine("{0} bad filesize",FATAL); return; } tmp = Path.GetTempFileName(); try { int gdfsx = GetDiskFreeSpaceEx( Path.GetPathRoot(tmp).ToString(), ref lpFreeBytesAvailable, ref lpTotalNumberOfBytes, ref lpTotalNumberOfFreeBytes ); } catch (Exception e) { Console.Error.WriteLine("{0} GetDiskFreeSpaceEx error",FATAL); return; } try { if (lpFreeBytesAvailable < count) throw new ArgumentOutOfRangeException(); } catch (Exception e) { Console.WriteLine("{0} bad filesize",FATAL); return; } using (fs = File.Create(tmp)) { try { fs.SetLength(count); } catch (Exception e) { Console.Error.WriteLine("{0} file creation error",FATAL); return; } try { fs.Flush(); } catch (Exception e) { Console.Error.WriteLine("{0} file flush error",FATAL); return; } } if (File.Exists(fn)) { try { File.Delete(fn); } catch (Exception e) { Console.Error.WriteLine("{0} could not remove existing file",FATAL); return; } } try { File.Move(tmp,fn); } catch (Exception e) { Console.Error.WriteLine("{0} file move failed",FATAL); File.Delete(tmp); } } private static void usage() { Console.Error.WriteLine("\n"); Console.Error.Write("FastFileAlloc"); Console.Error.Write(" version 0.04 "); Console.Error.WriteLine(" - make new files of a specific size"); Console.Error.Write("(C) 2004-2005 Toby Betts"); Console.Error.WriteLine(" "); Console.Error.WriteLine("\n"); Console.Error.WriteLine("FastFileAlloc is a tool to create a new file of a certain exact size."); Console.Error.WriteLine("\n"); Console.Error.WriteLine("Usage:"); Console.Error.WriteLine(" FastFileAlloc.exe filename filesize"); Console.Error.WriteLine("\n"); Console.Error.WriteLine("Examples:"); Console.Error.WriteLine(" FastFileAlloc.exe test.txt 12345678"); Console.Error.WriteLine(" FastFileAlloc.exe sixmegs.txt 6M"); Console.Error.WriteLine(" FastFileAlloc.exe one_kilobyte 1K"); Console.Error.WriteLine(" FastFileAlloc.exe empty.file 0"); } } }