0

Okay I know questions similar to this have been asked but none seem to adequately answer my situation. So I have this bit of code:

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>

and from the two writeLines you get:

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

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

but it continues to give this error:

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>

I imagine its because of the : but how am I supposed to reference the file without that?

this was my next try after someone suggested 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);

the result was the same

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

0 回答 0