C#System.IO.File.Copy问题

时间:2019-03-21 17:30:03

标签: c# system.io.file

因此,我需要制作一个快速的Windows Form应用程序来处理带有目录的文件列表,并将它们复制到新目录中。通常我会使用批处理文件来执行此操作

@echo off
mkdir "C:\Users\%username%\Desktop\inst"

:A
ping 127.0.0.1 -n 1 > nul
xcopy "C:\Users\%username%\Desktop\M14.0.1512.400-enu-x64.exe" "C:\Users\%username%\Desktop\inst" /y
xcopy "C:\Users\%username%\AppData\Local\Temp\vcredist.exe" "C:\Users\%username%\Desktop\inst" /y

GOTO A

我知道我没有使用最佳实践等,但是我想出了一个快速脚本来帮助加快工作速度。现在可以对几个文件执行此操作,但是我正在处理的某些应用程序有40多个文件需要复制,并且每次必须写入批处理文件有些麻烦。

因此,我拍了一个简单的WF应用程序,其中包含一个简单的输入字段和一个按钮来启动该过程。

用户将文件列表(带有路径和目录,例如C:\ foo \ bar \ hello.txt)放入输入字段,然后应用从文本框中获取每一行,然后将其推到列表中做一些基本的过滤,例如删除\ n,\ t,\ r并将字符串用双引号括起来(如果它们尚不存在的话)。

new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;

while (run)
{
    foreach (string path in paths)
    {
        Thread.Sleep(100);
        try
        {
            File.Copy(path, dir, true);
        }
        catch (Exception e)
        {
            g.log.WriteToLog("Failed to copy asset: " + e.ToString());

        }
    }
};

}).Start();

运行时,这是我在日志中得到的:

//LOGS
21/03/2019 11:25:56  -  Failed to copy asset: System.ArgumentException: Illegal characters in path.   at System.IO.LongPathHelper.Normalize(String path, UInt32 maxPathLength, Boolean checkInvalidCharacters, Boolean expandShortPaths)   at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)   at System.IO.Path.GetFullPathInternal(String path)   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)   at S2_PackagingTool.Application_Extractor.ExtractAssets() in C:\Users\xxxx\Desktop\GitHub\S2-EnvPrepTool\Application_Extractor.cs:line 98
21/03/2019 11:25:56  -  Path: "C:\Program Files\Freedom Scientific\Runtime JAWS\18.0\jrt.exe" Dir: "C:\Users\xxxx\Desktop\GitHub\S2-EnvPrepTool\bin\Debug\Extracted Assets"

日志中的第二行是path和dir变量的值转储。

当我运行不带while循环的代码并手动添加路径和目录时,例如

File.Copy(@"C:\foo\bar\hello.txt", @"C:\hello\world", true);

File.Copy("C:\\foo\\bar\\hello.txt", "C:\\hello\\world", true);

工作正常。

如果您想看到它,我还将介绍filter方法。请记住,这又快又脏,所以:

public string QuoteEncapsulationFilter(string s)
{
    s = s.Replace("\n", String.Empty);
    s = s.Replace("\r", String.Empty);
    s = s.Replace("\t", String.Empty);
    s = s.Replace("\\", "\\\\");

    if (!s.Contains("\""))
    {
        s = "\"" + s + "\"";
    }

    return s;
}

我试图到处寻找答案,但是没有运气,有人可以告诉我我在这里做错了什么。如果您需要我提供更多信息,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

您在File.Copy (string sourceFileName, string destFileName, bool overwrite);函数中缺少文件名。您的dir路径需要文件名。

https://docs.microsoft.com/en-us/dotnet/api/system.io.file.copy?view=netframework-4.7.2

说以下话:

  

destFileName   字符串目标文件的名称。这不能是   目录。

编辑: 要在评论中回答您的第二个问题:

new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;

while (run)
{
    foreach (string path in paths)
    {
        Thread.Sleep(100);
        try
        {
            var fileName = Path.GetFileName(path); // Get the file name
            var fullDestination = dir + fileName;  // Complete the uri
            File.Copy(path, fullDestination, true);
        }
        catch (Exception e)
        {
            g.log.WriteToLog("Failed to copy asset: " + e.ToString());

        }
    }
};

}).Start();