C# 如何避免异常进程无法访问该文件,因为另一进程正在使用该文件。在.net中
我正在使用下面的代码解压a.gz文件。但我过去常常得到C# 如何避免异常进程无法访问该文件,因为另一进程正在使用该文件。在.net中,c#,.net,file,exception,task-parallel-library,C#,.net,File,Exception,Task Parallel Library,我正在使用下面的代码解压a.gz文件。但我过去常常得到.NET异常: 进程无法访问该文件,因为其他进程正在使用该文件 如何纠正此问题? 使用的代码如下: private static void Decompress(FileInfo fileToDecompress) { using (FileStream originalFileStream = fileToDecompress.OpenRead()) { string currentFileName = fi
.NET
异常:
进程无法访问该文件,因为其他进程正在使用该文件
如何纠正此问题?使用的代码如下:
private static void Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length -
fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream =
new GZipStream(originalFileStream,
CompressionMode.Decompress))
{
decompressionStream.CopyTo(decompressedFileStream);
}
}
}
//File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));
}
调用代码如下所示
var list = ftp.GetFileList(remotepath);
//-------------------
DateTime dt = DateTime.Now;
string st = String.Format("{0:yyyyMMdd}", dt);//20161120
Task[] myTasks = new Task[list.Count];
int i = 0;
foreach (string item in list)
{
if (item.StartsWith("GExport_") && (!item.ToUpper().Contains("DUM")) && (item.Contains(st)) && (!item.ToUpper().Contains("BLK")))
{
4gpath = item;
//Downloadfile()
ftp.Get(dtr["REMOTE_FILE_PATH"].ToString() + 4gpath , @localDestnDir + "\\" + dtr["SOURCE_PATH"].ToString());
download_location_hw = dtr["LOCAL_FILE_PATH"].ToString();
// Spin off a background task to process the file we just downloaded
myTasks[i++] = Task.Factory.StartNew(() =>
{
//Extractfile()
ExtractZipfiles(download_location_hw + "//" + huwawei4gpath, dtr["REMOTE_FILE_PATH"].ToString(),
dtr["FTP_SERVER"].ToString(), dtr["FTP_USER_ID"].ToString(),
dtr["TECH_CODE"].ToString(), dtr["VENDOR_CODE"].ToString());
}
}
}
private static void ExtractZipfiles(string download_location_hw,string remotepath,string server,string userid,string tech,string vendor)
{
if (download_location_hw.Contains(".gz"))
{
DirectoryInfo directorySelected = new DirectoryInfo(Path.GetDirectoryName(download_location_hw));
foreach (FileInfo fileToDecompress in directorySelected.GetFiles("*.gz"))
{
Decompress(fileToDecompress);
}
}
}
您可能正在提取从同一文件夹中的任务下载的
zip
文件:
download_location_hw = dtr["LOCAL_FILE_PATH"].ToString();
var directorySelected = new DirectoryInfo(Path.GetDirectoryName(download_location_hw));
如果出于某种原因,有两个文件指向同一本地路径,则finalforeach
语句将与其他任务相交:
foreach (var fileToDecompress in directorySelected.GetFiles("*.gz"))
{
Decompress(fileToDecompress);
}
在这里,您将搜索文件夹中的所有*.gz
文件,即使是那些仍处于下载模式或正在由其他进程解压缩的文件。因此,您应该确定每个任务都在不同的文件夹中运行
如果您想简单地更正代码,可以尝试以下方法(此处的主要更改不是枚举所有存档文件,而是从任务参数中获取一个,带有):
不同的任务在相同的下载路径上运行,并导致冲突。 例如,任务1将a.gz、b.gz置于“MyDir”之下,而任务2将c.gz置于“MyDir”之下
但现在这两个任务都在运行,并试图在解压缩期间将文件重命名为a、b、c。因此,您似乎需要为每个任务创建子目录,或者为下载的文件名添加前缀\后缀和唯一标识符。能否显示调用此方法的代码?尤其是TPL bitsi发布了调用代码
myTasks
?如何更快地下载和Unizip大量文件?每个任务对应一个文件?peter更新了答案。你不应该再问同样的问题,因为你正在启动的并行任务是处理所有文件,而不是只处理下载的文件。
private static void ExtractZipfiles(string download_location_hw,string remotepath,string server,string userid,string tech,string vendor)
{
if (download_location_hw.Contains(".gz") && File.Exists(download_location_hw))
{
Decompress(new FileInfo(download_location_hw));
}
}