由网友(對妞、吹囗哨)分享简介:我需要创建一个包含随机数据,但有一个特定大小的文件。我想不出这样做的一个有效的方法。I need to create files that contain random data but are of a specific size. I cannot figure out a efficient way of do...
我需要创建一个包含随机数据,但有一个特定大小的文件。我想不出这样做的一个有效的方法。
I need to create files that contain random data but are of a specific size. I cannot figure out a efficient way of doing this.
目前,我试图用的BinaryWriter写一个空的字符数组到一个文件中,但我得到一个内存不够的异常试图创建阵列的具体尺寸时
Currently I am trying to use the BinaryWriter to write an empty char array to a file but I get an Out of Memory Exception when trying to create the array to the specific size
char[] charArray = new char[oFileInfo.FileSize];
using (BinaryWriter b = new BinaryWriter(File.Open(strCombined, FileMode.Create), System.Text.Encoding.Unicode))
{
b.Write(charArray);
}
建议?
感谢。
推荐答案
我真的需要使用这样的:
I actually needed to use this:
http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx
using (var fs = new FileStream(strCombined, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.SetLength(oFileInfo.FileSize);
}
oFileInfo是我要创建的文件的自定义文件信息对象。文件大小是它的大小为int。
oFileInfo is a custom file info object of the file I want to create. FileSize is it's size as an int.
感谢。
相关推荐
最新文章