我目前正在开发一个项目,其目标是对某些文件进行排序(图像文件,但未来可能是其他文件) 我当前的问题在于,在文件经过以下代码后,它会被卡住,而后来使用的File.delete和File.Move无法对文件进行操作。
到目前为止,我已经在堆栈溢出中经历了相当多的帖子,这些帖子已经摆弄了文件流参数,垃圾收集以及仅使用incase内部使用.Close()和.Dispose()。
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方法时会抛出Error,如下所示:
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++;
}
}
}
ImgShare和Netconn 是使用文件时创建的两个全局变量,当它们不再使用时会被处理。
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();
}