绝对路径中的非法性格

时间:2013-07-02 16:50:38

标签: c# file-io

好的,我知道有类似的问题已被问到,但似乎没有一个能够充分回答我的情况。所以我有这段代码:

String old = @"C:\emsdropbox\" + readData.Substring(0, readData.IndexOf("renamed"));
String newFile = @"C:\emsdropbox\" +  readData.Substring(readData.IndexOf("renamed") + 8, readData.Length-(readData.IndexOf("renamed") + 9));
Console.WriteLine(old + " : " + newFile);
old = Regex.Replace(old, @"\'", "");
newFile = Regex.Replace(newFile, @"\'", "");
old = old.Trim();
newFile = newFile.Trim();
Console.WriteLine(old + " : " + newFile);
System.IO.File.Move(old, newFile);</code>

从你得到的两个writeLines:

  1. 'C:\emsdropbox\finally2.txt' : 'C:\emsdropbox\finally3.txt'

  2. C:\emsdropbox\finally2.txt : C:\emsdropbox\finally3.txt

但它继续发出此错误:

mSystem.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.File.Move(String sourceFileName, String destFileName)
at TCPListener.getMessage()e this error </code>

我想象它是因为:但我怎么能在没有它的情况下引用该文件呢?

在有人建议使用path.combine后,这是我的下一次尝试:

String oldData = readData.Substring(0, readData.IndexOf("renamed"));
oldData = Regex.Replace(oldData, @"\'", "");
String newData =  readData.Substring(readData.IndexOf("renamed") + 8, readData.Length-(readData.IndexOf("renamed") + 9));
newData = Regex.Replace(newData, @"\'", "");
String old = System.IO.Path.Combine(@"C:\emsdropbox\",oldData);
String newFile = System.IO.Path.Combine(@"C:\emsdropbox\", newData);
System.IO.File.Move(old, newFile);

结果是相同的

illegal Characters in path
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.Combine(String path1, String path2)
at TCPListener.getMessage()

0 个答案:

没有答案