Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 由于特殊字符,文件移动操作失败_C#_.net_File_Special Characters_Move - Fatal编程技术网

C# 由于特殊字符,文件移动操作失败

C# 由于特殊字符,文件移动操作失败,c#,.net,file,special-characters,move,C#,.net,File,Special Characters,Move,我有一个文件移动操作,文件夹路径有特殊字符,如[,,(、~以及一些具有这些特殊字符的文件名。我从.csv文件中读取文件路径和名称。现在我希望移动这些文件,并且当我尝试通过c#移动时,路径错误中会出现非法字符。如何克服该错误并移动文件 while ((line = reader.ReadLine()) != null) { char[] delimiters = new char[] { ',' }; parts= line.Split(delimiters); if (p

我有一个文件移动操作,文件夹路径有特殊字符,如[,,(、~以及一些具有这些特殊字符的文件名。我从.csv文件中读取文件路径和名称。现在我希望移动这些文件,并且当我尝试通过c#移动时,路径错误中会出现非法字符。如何克服该错误并移动文件

while ((line = reader.ReadLine()) != null)
{
    char[] delimiters = new char[] { ',' };
    parts= line.Split(delimiters);
    if (parts.Length > 0)
    {
        SourceFilePathCSV = parts[0];
        SourceFileNameCSV = parts[1];
        filesize = parts[2];

        totalSourceFilePath = Path.Combine(SourceFilePathCSV ,SourceFileNameCSV);
        strDestinationDynamicPath = SourceFilePathCSV.Replace("\\\server\folder\\", " ").TrimEnd();
        strConstructedDestinationfullpath = Path.Combine(strDestinationStaticPath, strDestinationDynamicPath);
        if (!string.IsNullOrEmpty(strConstructedDestinationfullpath)) 
        {
            if (!Directory.Exists(strDestinationDynamicPath))
            {
                Directory.CreateDirectory(strConstructedDestinationfullpath);
                try
                {
                    if (File.Exists(totalSourceFilePath))
                    {
                        strtotaldestinationpath = Path.Combine(strConstructedDestinationfullpath, SourceFileNameCSV);
                        File.Move(totalSourceFilePath, strtotaldestinationpath);
                        string changed = Path.ChangeExtension(totalSourceFilePath, ".txt");
                        File.WriteAllText(changed, strTextInsidefile);
                        logMessage = "SourceFile = " + totalSourceFilePath + " DestinationFile = " + strConstructedDestinationfullpath + "  " + DateTime.Now.ToString() + "   " + loggeduserid.ToString() + " Input file = " + inputfile + "  " + "  Moved + " + "filesize = " + filesize;
                        WriteLog(logMessage);
                        filecountMoved = filecountMoved + 1;

                    }//file.exists(totalsource)
                    else
                    {
                        filecountnotmoved = filecountnotmoved + 1;
                        logMessage = "SourceFile = " + totalSourceFilePath + " DestinationFile = " + strConstructedDestinationfullpath + "  " + DateTime.Now.ToString() + "   " + loggeduserid.ToString() + " Input file = " + inputfile + "  " + "Not Moved " + "filesize = " + filesize;
                        WriteLog(logMessage);
                    }
                }
                catch (Exception ex)
                {
                    string strlogmessageex = ex.Message.ToString();
                    WriteLog(strlogmessageex);
                    return;
                }
                }//if directory not exists

        }//string or null
    }//parts.length
    filecount = filecount + 1;
}//while

您可以使用从返回的集合以编程方式从字符串中删除无效字符

但是,如果文件确实存在,则必须引入无效字符。除非是复制/粘贴错误,否则可能是这一行:

strDestinationDynamicPath = SourceFilePathCSV.Replace("\\\server\folder\\", " ").TrimEnd();
开头有三个斜杠。“\\”是一个字符,“\s”是另一个字符。但这可能是复制/粘贴错误

但是你要用一个空格替换它,这个空格对于文件或文件夹的第一个字符是无效的。
TrimEnd
只删除结尾的空格


如果不是这样,则调试并查看strConstructedDestinationfullpath的值,并查找任何异常情况。

您是否尝试在操作系统中手动创建这些文件夹?我认为您不能这样做。这是操作系统限制,而不是编程问题。这些不是文件的无效字符。您确定吗问题不在于其他字符?你能显示它失败的确切文件名吗?@ntohl,这些文件夹不是手动创建的,我的程序所做的只是将文件夹中的某些文件移动到其他位置。我同意,我不确定如何允许文件夹/文件也使用特殊字符创建。@juharr,这是一个失败的文件。@n\\server\share\legal\xxxxxxx,xxxxx\x.docs from CS(记录2014年12月)\OPCC\CS的office\DLS acct.Forms\KLtrReTransferToTrust(062323).doc。其中,路径/文件夹名为\\server\share\legal\xxxxx,xxxx\x.docs from CS(记录2014年12月)\OPCC\CS的office\DLS acct.Forms\文件名为KLtrReTransferToTrust(062323).docyou是对的,\\\是一个复制粘贴错误。我可以移动没有特殊字符的文件。只有当文件夹/文件名包含“(,{,~,[”等时,它才会失败