C# &引用;进程无法访问文件XXXX,因为它正被另一进程使用;被file.copy使用后
我目前正在从事一个项目,目标是对一些文件(图像文件,但将来可能是其他文件)进行排序 我当前的问题在于,文件经过以下代码后会被卡住,并且稍后使用的file.delete和file.Move无法对文件执行操作 到目前为止,我已经在这里读了很多关于堆栈溢出的文章,这些文章都是在处理Filestream参数、垃圾收集和使用.Close()和.Dispose()来防止堆栈溢出C# &引用;进程无法访问文件XXXX,因为它正被另一进程使用;被file.copy使用后,c#,file,process,C#,File,Process,我目前正在从事一个项目,目标是对一些文件(图像文件,但将来可能是其他文件)进行排序 我当前的问题在于,文件经过以下代码后会被卡住,并且稍后使用的file.delete和file.Move无法对文件执行操作 到目前为止,我已经在这里读了很多关于堆栈溢出的文章,这些文章都是在处理Filestream参数、垃圾收集和使用.Close()和.Dispose()来防止堆栈溢出 public void CopyFile(string source, string destination) {
public void CopyFile(string source, string destination)
{
GC.Collect();
try
{
using (imgShare)
{
using (netConn)
{
var sourcePath = $"{imgShare.GetFolderPath(Folder.Storage)}\\Item\\";
var oldFile = $"{sourcePath}{source}";
var tmpFileName = $"{sourcePath}{destination}";
}
}
GC.Collect();
using (imgShareLegacy)
{
using (netConnLegacy)
{
var sourcePath = $"{imgShareLegacy.GetFolderPath(Folder.Storage)}\\item\\";
var oldFile = $"{sourcePath}{source}";
var tmpFileName = $"{sourcePath}{destination}";
CopyFiles(oldFile, tmpFileName);
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR OCCURRED AT {DateTime.Now.ToString("T")} inside CopyFile: {ex} | EX-MESSAGE: {ex.Message} | SOURCE: {source} | DEST: {destination}");
}
}
private void CopyFiles(string oldFile, string tmpFileName)
{
try
{
using (FileStream fs = new FileStream(oldFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
File.Copy(fs.Name, tmpFileName, true);
File.SetAttributes(tmpFileName, FileAttributes.Normal);
fs.Dispose();
fs.Close();
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR OCCURRED AT {DateTime.Now.ToString("T")} inside CopyFile: {ex} | EX-MESSAGE: {ex.Message} | OLDFILE: {oldFile} | TMPFILENAME: {tmpFileName}");
}
}
我很抱歉代码如此混乱,我一直在尝试一些不同的方法来让它工作。最新的一次尝试是将代码拆分,但希望最小
在CopyFile方法之后调用DeleteFile方法时,会在DeleteFile方法内部引发错误,如下所示:
public void PromoteTempFile(IEnumerable<ImgNameChanges> tmpNameChanges)
{
var nameChanges = tmpNameChanges.ToArray();
try
{
var sourcePath = $"{imgShare.GetFolderPath(Folder.Storage)}\\item\\";
GC.Collect();
GC.WaitForPendingFinalizers();
for (int i = 0; i < tmpNameChanges.Count(); i++)
{
var oldFile = $"{sourcePath}{nameChanges[i].OldVal}";
DeleteFile(oldFile, imgShare, netConn);
}
GC.Collect();
for (int i = 0; i < tmpNameChanges.Count(); i++)
{
var tmpFile = $"{sourcePath}{nameChanges[i].NewVal}";
MoveFile(tmpFile, imgShare, netConn);
var sourcePathLegacy = $"{imgShareLegacy.GetFolderPath(Folder.Storage)}\\item\\";
for (int i = 0; i < tmpNameChanges.Count(); i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
var oldFile = $"{sourcePathLegacy}{nameChanges[i].OldVal}";
DeleteFile(oldFile, imgShareLegacy, netConnLegacy);
}
GC.Collect();
for (int i = 0; i < tmpNameChanges.Count(); i++)
{
var tmpFile = $"{sourcePathLegacy}{nameChanges[i].NewVal}";
MoveFile(tmpFile, imgShareLegacy, netConnLegacy);
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR OCCURRED AT {DateTime.Now.ToString("T")} inside PromoteToFile: {ex} | EX-MESSAGE: {ex.Message} | TMPFILE: {tmpNameChanges}");
}
}
private void MoveFile(string tmpFile, ThgEasyImageShareAccess imgAccess, NetworkConnection netConn)
{
var errCount = 0;
while (errCount <= 1)
{
try
{
using (imgAccess)
{
using (netConn)
{
using (FileStream fs = new FileStream(tmpFile, FileMode.Open, FileAccess.Read, FileShare.Delete))
{
File.Move(fs.Name, fs.Name.Replace("tmp_", string.Empty));
fs.Dispose();
fs.Close();
errCount = 5;
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR OCCURRED AT {DateTime.Now.ToString("T")} inside MoveFile: {ex} | EX-MESSAGE: {ex.Message} | TMPFILE: {tmpFile} | IMG ACCESS: {imgAccess} | NETCONN: {netConn}");
errCount++;
}
}
}
private void DeleteFile(string oldFile, ThgEasyImageShareAccess imgAccess, NetworkConnection netConn)
{
// At loope de filer der sidder fast igennem, virker ikke da de stadig sidder fast bagefter
var errCount = 0;
while (errCount <= 1)
{
try
{
using (imgAccess)
{
using (netConn)
{
using (FileStream fs = new FileStream(oldFile, FileMode.Open,FileAccess.Read, FileShare.Delete))
{
if (File.Exists(fs.Name))
{
File.Delete(fs.Name);
}
fs.Dispose();
fs.Close();
errCount = 5;
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine($"ERROR OCCURRED AT {DateTime.Now.ToString("T")} inside DeleteFile: {ex} | EX-MESSAGE: {ex.Message} | OLDFILE: {oldFile} | IMGACCESS: {imgAccess} | NETCONN: {netConn}");
errCount++;
}
}
}
为什么要使用流来复制文件?file.copy(“old”、“new”)将为您复制该文件。添加该文件是为了能够使用以确保文件已被处理。在操作完成之后。好吧,真的很混乱…打开流(如果文件不是fount将引发异常),然后检查文件是否存在,然后删除它。你为什么还要开小溪?你真的需要这么多的
GC.Collect()
调用吗?所有使用的,你真的需要所有的吗?在NetworkConnection
上使用可能会在最后关闭。是的,我知道它很凌乱,在过去的几年中它一直在慢慢建立。。一两周,我一直在尝试不同的方法来解决这个问题。现在我很可能不需要那么多GC,但我已经非常绝望地寻找解决方案。启动流的目的是希望能够在使用后处理文件,以便在我开始看到问题时修复开始时的错误。原始代码将非常清晰易懂,现在它很混乱,会产生更多不必要的问题。为什么要使用流复制文件?file.copy(“old”、“new”)将为您复制该文件。添加该文件是为了能够使用以确保文件已被处理。在操作完成之后。好吧,真的很混乱…打开流(如果文件不是fount将引发异常),然后检查文件是否存在,然后删除它。你为什么还要开小溪?你真的需要这么多的GC.Collect()
调用吗?所有使用的,你真的需要所有的吗?在NetworkConnection
上使用可能会在最后关闭。是的,我知道它很凌乱,在过去的几年中它一直在慢慢建立。。一两周,我一直在尝试不同的方法来解决这个问题。现在我很可能不需要那么多GC,但我已经非常绝望地寻找解决方案。当我开始看到这个问题时,这个流是希望能够在使用后处理文件,以修复开始时的错误。原始代码会非常清晰易懂,现在它很混乱,会产生更多不必要的问题。
private ThgEasyImageShareAccess imgShare;
private NetworkConnection netConn;
private ThgEasyImageShareAccess imgShareLegacy;
private NetworkConnection netConnLegacy;
public ImageRepository()
{
imgShare = new ThgEasyImageShareAccess(SourceName.MedieServer);
netConn = new NetworkConnection(imgShare.GetFolderPath(Folder.Storage), new NetworkCredential(imgShare.Username, imgShare.Password, imgShare.Domain));
imgShareLegacy = new ThgEasyImageShareAccess(SourceName.MedieServerLegacy);
netConnLegacy = new NetworkConnection(imgShareLegacy.GetFolderPath(Folder.Storage), new NetworkCredential(imgShareLegacy.Username, imgShareLegacy.Password, imgShareLegacy.Domain));
}
~ImageRepository()
{
imgShare.Dispose();
netConn.Dispose();
imgShareLegacy.Dispose();
netConnLegacy.Dispose();
}