我有一个同步软件,可以从" Incoming"中加载CSV文件。文件夹,处理它们然后将它们移动到"存档"文件夹中。
今天,我看到此同步软件出现以下错误:
[23/06/2014 00:06:04 AM]:无法从
移动文件D:\ IBI_ORDER_IMPORTER_FTP_SERVER \ Template3 \ Fifty& Dean \ Incoming \ 5A040K ___ d6f1ca45937b4ceb98d29d0db4601bf4.csv to
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"));
}
知道这个错误意味着什么吗?以及如何规避它?
我写了一个上面的减少版本来在受控环境中测试它,我没有得到这个代码的错误:
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();
}
答案 0 :(得分:2)
您需要包含检查以确保路径在运行时存在并检查输出,这非常简单:
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);
我建议使用这种方法的原因是由于多任务操作系统在多任务操作系统下暂时可用路径的可能性取决于多种因素:网络连接,权限(在任何时候由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);
}
}
答案 1 :(得分:0)
我有这个
无法找到路径的一部分
当我时发生在我身上File.Move(mapfile_path , Path.Combine(newPath, mapFile));
。经过一些测试,我发现我们的服务器管理员已阻止任何用户应用程序写入[newPath]中的该目录!
因此,右键单击该目录以观察“安全”选项卡上的权限矩阵,以查看阻止您的任何内容。