我有一个同步软件,它从“传入”文件夹加载 CSV 文件,处理它们,然后将它们移动到“存档”文件夹。
今天,我看到这个同步软件出现以下错误:
[23/06/2014 00:06:04 AM]:无法从
D:\IBI_ORDER_IMPORTER_FTP_SERVER\Template3\Fifty & Dean\Incoming\5A040K___d6f1ca45937b4ceb98d29d0db4601bf4.csv 到
D:\IBI_ORDER_IMPORTER_FTP_SERVER\Template3\Fifty & Dean\Archive\5A040K___d6f1ca45937b4ceb98d29d0db4601bf4.csv -找不到路径的一部分。
这是从同步软件中提取的片段,文件在其中被处理和移动:
public static void ProcessSingleUserFile(Int32 TemplateId, String ImportedBy, String FilePath)
{
// Always Rename File To Avoid Conflict
string FileName = Path.GetFileNameWithoutExtension(FilePath);
String NewFilePath = FilePath.Replace(FileName, Utils.RandomString() + "___" + FileName);
File.Move(FilePath, NewFilePath);
FilePath = NewFilePath;
// Log
SyncUtils.ConsoleLog(String.Format("Processing [ {0} as {1} ] By [ {2} ] On Template [ #{3} ]",
FileName + ".csv",
Path.GetFileName(FilePath),
ImportedBy,
TemplateId));
// Init
List<OrderDraft> myOrderDrafts = new List<OrderDraft>();
// Parsed Based On Template Id
if (TemplateId == Settings.Default.Multi_Order_Template_Id)
{
// Try Parse File
myOrderDrafts = Utils.ParseMultiImportFile(TemplateId, ImportedBy, FilePath, true);
}
else
{
// Try Parse File
myOrderDrafts.Add(Utils.ParseImportFile(TemplateId, ImportedBy, FilePath, true));
}
// Process Orders
foreach (OrderDraft myOrderDraft in myOrderDrafts)
{
/* code snipped */
}
// Archive File
File.Move(FilePath, FilePath.Replace("Incoming", "Archive"));
}
Run Code Online (Sandbox Code Playgroud)
知道这个错误是什么意思吗?以及如何规避它?
我写了一个上面的简化版本来在受控环境中测试这个,我没有收到这个代码的错误:
static void Main(string[] args)
{
try
{
string baseDir = @"C:\Users\Administrator\Desktop\FTP_SERVER\Template3\Fifty & Dean\Incoming\";
string[] filePaths = Directory.GetFiles(baseDir, "*.csv");
foreach (string filePath in filePaths)
{
// do some work here ...
// move file
string newFilePath = filePath.Replace("Incoming", "Archive");
File.Move(filePath, newFilePath);
Console.WriteLine("File successfully moved");
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
Console.ReadKey();
}
Run Code Online (Sandbox Code Playgroud)
您需要进行检查以确保路径在运行时存在并检查输出,非常简单,例如:
if(!Directory.Exists(Path.GetDirectoryName(filePath)))
{
Console.WriteLine("filePath does not exist: " + filePath);
}
if(!Directory.Exists(Path.GetDirectoryName(newFilePath)))
{
Console.WriteLine("newFilePath does not exist: " + newFilePath);
}
File.Move(filePath, newFilePath);
Run Code Online (Sandbox Code Playgroud)
我建议这种方法的原因是,在多任务操作系统下,路径可能会暂时变得可用或不可用,具体取决于多种因素:网络连接、权限(随时由 GPO 推送)、防火墙规则、 AV 排除被吹走等。即使 CPU 或 RAM 不足也可能会产生问题。简而言之,如果您只在事后检查路径可用性,那么您永远不知道代码运行时究竟发生了什么。
或者,如果您的问题是间歇性的,您可以尝试捕获错误并将信息写入某种日志,如下所示:
try
{
File.Move(filePath, newFilePath);
}
catch(Exception ex)
{
if(!Directory.Exists(Path.GetDirectoryName(filePath)))
{
Console.WriteLine("filePath does not exist: " + filePath);
}
if(!Directory.Exists(Path.GetDirectoryName(newFilePath)))
{
Console.WriteLine("newFilePath does not exist: " + newFilePath);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7483 次 |
最近记录: |