FileStream 很慢吗?
我试图将 5 GB ISO 文件复制到具有 29 GB 可用空间的 32 GB 闪存驱动器上。
Windows 7 拒绝让我将文件拖放到到闪存驱动器上,报告该文件对于目标文件系统来说太大。
我最终了解到这是因为驱动器格式化为 FAT32 而不是 NTFS,但在我编写此例程来复制文件之前:
private void copyFile(string from, string to) {
bool ok = true;
using (StreamReader sr = new StreamReader(from, true)) {
using (StreamWriter sw = new StreamWriter(to, false, sr.CurrentEncoding)) {
int FOUR_K = 4048;
char[] buf = new char[FOUR_K];
try {
while (-1 < sr.Peek()) {
int len = sr.Read(buf, 0, FOUR_K);
sw.Write(buf, 0, len);
sw.Flush();
}
}
catch (Exception err) {
ok = false;
throw err;
}
}
}
if (ok) {
Console.WriteLine("Done!");
}
}
我让它运行大约一个小时,文件大小仅达到 270 MB。
这是怎么回事?
我的代码中的什么导致我的文件花费这么长时间?
这是我选择的 FOUR_K
变量大小吗?
[更新]
我有两个 ISO 文件: Win8-32位位于~3GB 和 Win8-64bit-Developer ~5 GB。使用拖放操作,Windows 资源管理器在大约三分钟内将 3 GB 文件复制到我的 32 GB 闪存驱动器。
使用 Marc Gravell 的技术,我再次进行了此操作:
[STAThread]
static void Main(string[] args) {
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Title = "Select File";
if (dlg.ShowDialog() == DialogResult.OK) {
using (FolderBrowserDialog fdg = new FolderBrowserDialog()) {
fdg.Description = "Select Destination";
if (fdg.ShowDialog() == DialogResult.OK) {
DateTime start = DateTime.Now;
Console.WriteLine("Started at {0:g}.\nWorking...", start);
using (FileStream fin = File.Open(dlg.FileName, FileMode.Open)) {
using (FileStream fout = new FileStream(Path.Combine(fdg.SelectedPath, dlg.SafeFileName), FileMode.Create)) {
try {
fin.CopyTo(fout);
} catch (Exception err) {
Console.WriteLine("An Error Occurred:");
Console.WriteLine(err.Message);
}
}
}
DateTime end = DateTime.Now;
TimeSpan span = (end - start);
Console.WriteLine("Process Ended at {0}.\nThe total minutes passed = {1}.", end, span.TotalMinutes);
Console.WriteLine("Press Any Key.");
Console.ReadKey();
}
}
}
}
}
使用 FileStream
在上面的实例中,程序运行了大约 8 小时,准确复制了 4,194,300 KB
,然后抛出内存不足异常。
I was trying to copy a 5 GB ISO file onto a 32 GB flash drive with 29 GB of free space.
Windows 7 refused to let me drag-and-drop the file onto the flash drive, reporting the file was too large for the destination file system.
I eventually learned this was because the drive was formatted as FAT32 instead of NTFS, but not before I wrote this routine to copy the file over:
private void copyFile(string from, string to) {
bool ok = true;
using (StreamReader sr = new StreamReader(from, true)) {
using (StreamWriter sw = new StreamWriter(to, false, sr.CurrentEncoding)) {
int FOUR_K = 4048;
char[] buf = new char[FOUR_K];
try {
while (-1 < sr.Peek()) {
int len = sr.Read(buf, 0, FOUR_K);
sw.Write(buf, 0, len);
sw.Flush();
}
}
catch (Exception err) {
ok = false;
throw err;
}
}
}
if (ok) {
Console.WriteLine("Done!");
}
}
I let it run for about an hour, and it only got to 270 MB in file size.
What's going on?
What in my code is causing my file to take so long?
Is it my choice of the FOUR_K
variable size?
[UPDATE]
I have two ISO files: Win8-32bit at ~3 GB and Win8-64bit-Developer at ~5 GB. Using drag-and-drop, Windows Explorer copied the 3 GB file to my 32 GB flash drive in about three minutes.
Using Marc Gravell's technique, I went at this yet again:
[STAThread]
static void Main(string[] args) {
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.Title = "Select File";
if (dlg.ShowDialog() == DialogResult.OK) {
using (FolderBrowserDialog fdg = new FolderBrowserDialog()) {
fdg.Description = "Select Destination";
if (fdg.ShowDialog() == DialogResult.OK) {
DateTime start = DateTime.Now;
Console.WriteLine("Started at {0:g}.\nWorking...", start);
using (FileStream fin = File.Open(dlg.FileName, FileMode.Open)) {
using (FileStream fout = new FileStream(Path.Combine(fdg.SelectedPath, dlg.SafeFileName), FileMode.Create)) {
try {
fin.CopyTo(fout);
} catch (Exception err) {
Console.WriteLine("An Error Occurred:");
Console.WriteLine(err.Message);
}
}
}
DateTime end = DateTime.Now;
TimeSpan span = (end - start);
Console.WriteLine("Process Ended at {0}.\nThe total minutes passed = {1}.", end, span.TotalMinutes);
Console.WriteLine("Press Any Key.");
Console.ReadKey();
}
}
}
}
}
Using the FileStream
instances above, the program ran for about 8 hours, copied exactly 4,194,300 KB
and then threw an Out Of Memory Exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道性能问题(听起来很奇怪),但是根本没有理由在这里使用
StreamReader
/StreamWriter
,因为你可以只在二进制级别复制。事实上,ISO 映像不是文本,因此将其读入char
数据很可能会损坏内容。有关信息,即使您不想使用File.Copy
,这里你需要的是:一小时达到270k需要努力(除非IO基本死掉);我的猜测是它在某个地方抛出了错误。
I don't know about the performance issue (sounds very odd), but there's no reason the use a
StreamReader
/StreamWriter
here at all, since you could just copy at the binary level. Indeed, an ISO image is not text, so reading it intochar
data is very likely to corrupt things. For info, even if you don't want to useFile.Copy
, all you need here is:To get 270k in an hour you need to try hard (unless the IO is basically dead); my guess is it threw an error somewhere.
我怀疑每次都是
Flush
调用。如果您使用的是 .NET 4,Stream 现在有一个
CopyTo
方法:I suspect it's the
Flush
call every time.If you are using .NET 4, Stream now has a
CopyTo
method:如果您尝试复制二进制文件,为什么要使用 StreamReader/StreamWriter?尝试这样的事情:
If you are trying to copy a binary file, why do you use
StreamReader/StreamWriter
? Try something like this: