Creating a Huge Dummy File in a Matter of Seconds in C#

后端 未结 5 395
梦如初夏
梦如初夏 2020-12-04 21:43

I want to create a huge dummy file say 1~2 GBs in matter of seconds. here is what I\'ve written in C# :

file.writeallbytes(\"filename\",new byte[a huge numbe         


        
相关标签:
5条回答
  • 2020-12-04 22:01

    If you don't care about the contents, then by far the fastest way I know of is this - it is practically instant:

    private void CreateDummyFile(string fileName, long length)
    {
        using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            fileStream.SetLength(length);
        }
    }
    
    0 讨论(0)
  • 2020-12-04 22:02

    Simply create the file, seek to a suitably large offset, and write a single byte:

    FileStream fs = new FileStream(@"c:\tmp\huge_dummy_file", FileMode.CreateNew);
    fs.Seek(2048L * 1024 * 1024, SeekOrigin.Begin);
    fs.WriteByte(0);
    fs.Close();
    

    This will yield a 2GB file with basically unpredictable contents, which should be fine for your purposes.

    0 讨论(0)
  • 2020-12-04 22:05

    Why did you not use the BackgroundWorker class to achieve this, as you can pass anything into the method ReportProgress to indicate the status report. See the example below:

            private BackgroundWorker bgWorker;
            public Form1()
            {
                InitializeComponent();
                bgWorker = new BackgroundWorker();
                bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
                bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
                bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
                bgWorker.RunWorkerAsync();
            }
    
            void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
               this.label2.Text = "Done";
            }
    
            void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
               MyStatus myProgressStatus = (MyStatus)e.UserState;
               this.label2.Text = string.Format("segments write : {0}" + Environment.Newline + "Segments Remain: {1}", myProgressStatus.iWritten, myProgressStatus.iRemaining);
            }
    
            void bgWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                long FSS = din.TotalFreeSpace;
                    long segments = FSS / 10000;
                    long last_seg = FSS % 10000;
                    BinaryWriter br = new BinaryWriter(fs);
    
                    for (long i = 0; i < segments; i++)
                    {
                        br.Write(new byte[10000]);
    bgWorker.ReportProgress(i.ToString(), new MyStatus(i, ((segments-i) + 1)));
    
                    }
                    br.Write(new byte[last_seg]);
                    br.Close();
            }
    
    public class MyStatus{
       public int iWritten;
       public int iRemaining;
       public MyStatus(int iWrit, int iRem){
         this.iWritten = iWrit;
         this.iRemaining = iRem;
       }
    }
    }
    

    This is a rough draft...

    0 讨论(0)
  • 2020-12-04 22:11

    If you just need a FileStream, you could use FileStream.SetLength. That will get you a stream which is 2 GB long. Then you can write the final byte at an arbitrary position of your choice. But the contents will be undefined.

    If you're trying to actually create a file on the disk, yes, you'll need to actually write its contents. And yes, hard disks are going to be slow; something like a 1 GB/min write speed isn't totally ridiculous. Sorry -- that's physics!

    0 讨论(0)
  • 2020-12-04 22:21

    I could be wrong but you will probably find that it's impossible to create a file that large that quickly as there will be a bottleneck in the I/O writing process.

    However in your code above the Applciation.DoEvents will be slowing things down. Also any repainting of the screenthis.label2.Text = will cause a slight slow down.

    0 讨论(0)
提交回复
热议问题