建立在C#在几秒内一个巨大的虚拟文件 [英] Creating a Huge Dummy File in a Matter of Seconds in C#

查看:112
本文介绍了建立在C#在几秒内一个巨大的虚拟文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个庞大的虚拟文件说,1〜2 GB的在几秒钟的事。
以下是我已经写在C#中:

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 number]);

和与指示状态的另一种方式,就像下面这样:

and another way with indicating the status, was like following :

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]);

    this.label2.Text = "segments write :" + i.ToString() + "\r\n" + "segments remain :" + ((segments-i)+1).ToString();
    Application.DoEvents();
}
br.Write(new byte[last_seg]);
this.label2.Text += "\r\nDone!";
br.Close();

在那里喧嚣的磁盘信息对象

where din is Disk Information object

以及与这两个方法需要像2分钟或更长时间来写这么大的,但虚拟文件。有没有其他更快捷的方式这样做的?

well with these two approach it takes something like 2 or more minutes to write such a big but dummy file. is there any other faster way for doing so?

问候。

推荐答案

只需创建该文件,寻求适当大的偏移,并写一个单字节:

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();

这将产生具有基本未predictable内容2GB的文件,这应该是罚款的目的。

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

这篇关于建立在C#在几秒内一个巨大的虚拟文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆