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()
staticFlow
  • 141
  • 1
  • 2
  • 13
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 02 '13 at 16:51
  • Check this answer http://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames – Carlos Ferreira Jul 02 '13 at 16:53
  • Use path combine for file paths. The problem is probably the ' characters – Sayse Jul 02 '13 at 16:54
  • http://pastebin.com/a0Wm41yf this code works fine, but my string at the end is "C:\\asd2.txt", how it's possible? – Kamil Budziewski Jul 02 '13 at 18:27
  • there are no illegal characters in the string other than the : after the C but thats required it has been scrubbed of ' in the lines previous. I tried the solution with combine and still it is not working. – staticFlow Jul 02 '13 at 19:11
  • what the value of readData? – renefc3 Jul 02 '13 at 19:25
  • I try with "finally2.txtrenamedaaa" and works – renefc3 Jul 02 '13 at 19:25
  • the value of readData prior to sanitaion is 'Brandon2.txt' but when i put it into the combine method it is Brandon2.txt without the ' – staticFlow Jul 02 '13 at 19:52
  • haha so i did some checking and for some reason which iv'e yet to investigate the trim did not remove a bunch of tabs at the end of the string. So thats why it kept flagging. – staticFlow Jul 02 '13 at 20:06

0 Answers0