.net 复制文件时,出现文件正由另一进程使用,因此该进程无法访问该文件.原因是我用了openfiledialog选出文件来,然后再复制选出的文件.
如下string sourceDir = @"F:\文件";
string backupDir = @"F:\文件\yd";
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog()==DialogResult.OK)
{
try
{
sfilename = openDialog.FileName;
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
} }
openDialog.Dispose();
openDialog = null;
GC.Collect();
try
{
File.Copy(Path.Combine(sourceDir, sfilename), Path.Combine(backupDir, sfilename), true);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
出现的错误:
Error:Could not read file from disk.Original error: 文件 “f:\文件\cj.xls" 正由另一进程使用,因此该进程无法访问该文件。
附:用openfiledialog 打开文件是就是选的 F:\文件 目录下面的 文件.如果不用openfiledialog选择文件,直接指定文件名,就可以复制.
如下string sourceDir = @"F:\文件";
string backupDir = @"F:\文件\yd";
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog()==DialogResult.OK)
{
try
{
sfilename = openDialog.FileName;
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
} }
openDialog.Dispose();
openDialog = null;
GC.Collect();
try
{
File.Copy(Path.Combine(sourceDir, sfilename), Path.Combine(backupDir, sfilename), true);
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
出现的错误:
Error:Could not read file from disk.Original error: 文件 “f:\文件\cj.xls" 正由另一进程使用,因此该进程无法访问该文件。
附:用openfiledialog 打开文件是就是选的 F:\文件 目录下面的 文件.如果不用openfiledialog选择文件,直接指定文件名,就可以复制.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货