6

I have a sync software, which loads CSV files from "Incoming" folder, processes them and then moves them to the "Archive" folder.

Today, I saw the following error with this sync software:

[23/06/2014 00:06:04 AM] : Failed to move file from

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 - Could not find a part of the path.

Here's a snippet taken out of the sync software, where the file is processed and moved:

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"));
}

Any idea what this error means? and how to circumvent it?


I wrote a cut down version of the above to test this in a controlled environment and I am not getting the error with this code:

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();
}
4

3 回答 3

3

您需要包括检查以确保路径在运行时存在并检查输出,非常简单,例如:

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);
    }
}
于 2016-06-14T17:01:21.227 回答
1

File.Move如果使用的参数长于 .NET Framework 中的 MAX_PATH (260),也可能引发“找不到路径的一部分”异常。

因此,我在传递给之前使用长路径语法预先添加了我使用的路径File.Move并且它起作用了。

// Prepend long file path support
if( !packageFile.StartsWith( @"\\?\" ) )
    packageFile = @"\\?\" + packageFile;

请参阅: 如何处理名称超过 259 个字符的文件?

于 2021-08-24T11:16:42.403 回答
0

我有这个

找不到路径的一部分

当我发生在我身上

File.Move(mapfile_path , Path.Combine(newPath, mapFile));

. 经过一些测试,我发现我们的服务器管理员已阻止任何用户应用程序写入该 [newPath] 中的该目录!

因此,右键单击该目录以观察安全选项卡上的权限矩阵,以查看任何会阻止您的内容。

于 2017-10-30T20:49:38.610 回答