创建一个具有特定尺寸的新文件 [英] Create new file with specific size
问题描述
我需要创建一个包含随机数据,但有一个特定大小的文件。我想不出这样做的一个有效的方法。
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:
<一个href="http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx">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.
感谢。
这篇关于创建一个具有特定尺寸的新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!