4

I am unable to rename a file located in (D drive) using c#. I am getting the error

Exception Details: System.NotSupportedException: The given path's format is not supported.

every time.

I am using

string oldfilename = @"D:\abc\file.txt";
string newfilename = @"D:\abc\tree.txt";
System.IO.File.Move(oldfilename, newfilename);

but I am getting the error on the last line. I also tried changing the first 2 lines to

string oldfilename = "D:\\abc\\file.txt";
string newfilename = "D:\\abc\\tree.txt";

I also ensured that the file "file.txt" exists. Tried to use different location.

I also tried Reading contents of the file.txt, but I am getting same error. I searched all the Questions on SO, But with no luck I could get this issue resolved. I think there is some issue with the ":" that I am using after the drive letter while specifying the path. Please guide me.

BhushanK
  • 1,205
  • 6
  • 23
  • 39
Thundrstorm
  • 181
  • 3
  • 16

3 Answers3

0

I tried this on another computer but still it didn't work!(Surprising). I have managed to solve this issue by moving "file.txt" to my project folder. Now I am using

string oldfilename = "file.txt";
string newfilename = "tree.txt";
System.IO.File.Move(oldfilename, newfilename);

And this Works! This doesn't seems to be an answer to this question(for me), but It has really worked for me.

Thundrstorm
  • 181
  • 3
  • 16
  • This doesn't answer anything. Both relative and absolute paths work with File.Move. In fact, if *this* works, it would mean that there were invalid Unicode characters in the folder path (`D:\abc`) but not the file name – Panagiotis Kanavos Sep 18 '15 at 14:15
0

Why don´t you utilize:

 My.Computer.Filesystem.RenameFile("D:\file.txt", "tree.txt")
David BS
  • 1,822
  • 1
  • 19
  • 35
-2

I try it by myself and these variants worked:

        string oldfilename = "C:\\Users\\User\\Downloads\\WorkTemp\\file.txt";
        string newfilename = "C:\\Users\\User\\Downloads\\WorkTemp\\file2.txt";
        System.IO.File.Move(oldfilename, newfilename);


        string oldfilename = @"C:\Users\User\Downloads\WorkTemp\file1.txt";
        string newfilename = @"C:\Users\User\Downloads\WorkTemp\file2.txt";
        System.IO.File.Move(oldfilename, newfilename);

According to the reference source: http://referencesource.microsoft.com/#mscorlib/system/io/filestream.cs#749

NotSupportedException will be thrown if the index of the : in your path is at the third position or later. (One would expect : to be the second character) Are you sure there are no zero-width combining characters or other similar Unicode shenanigans going on in your source? from that answer

Community
  • 1
  • 1
Dima Adas
  • 300
  • 2
  • 10