3

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.

quyleanh
  • 63
  • 5
  • The `~` is used in 8.3 paths (like dos, 8 characters + 3 extension), Windows uses it internally for the 8.3 names of folder/file names > 8 characters. I guess that's the reason why you can't use it. – René Vogt Jul 12 '18 at 15:01
  • possibly Path.Combine is what u need to use here https://stackoverflow.com/questions/7348768/the-given-paths-format-is-not-supported – Fuzzybear Jul 12 '18 at 15:01

1 Answers1

7

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.

Agat
  • 4,577
  • 2
  • 34
  • 62
Owen Pauling
  • 11,349
  • 20
  • 53
  • 64