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

后端 未结 5 394
梦如初夏
梦如初夏 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);
        }
    }
    

提交回复
热议问题