Search code examples
c#renametilde

Rename to folder with tilde symbol


I know the method Directory.Move(source, destination) to rename a folder. But when I rename the desitnation folder with the ~ symbol at the begin, there is error.

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

Here is my code:

string oldFolderPath = @"C:\Old";
string newFolderPath = "~" + oldFolderPath;
Directory.Move(oldFolderPath, newFolderPath);

In the System.IO.Path.InvalidPathChars, the following characters are invalid: " < > |

It does not list tilde symbol, so how can I do? Thank you very much.


Solution

  • Tilde is perfectly valid in a folder name. Your code sample doesn't show what value demoPath has but, since tilde is valid in the folder name, you're probably doing something like appending it to the start of the path and not where you intended to.

    E.g. not like this: ~C:\Old but C:\~Old.