In C #, how can I prepare a string that will be valid for a Windows directory name

I am writing a C # program that reads certain tags from files and creates a directory structure based on tag values.

Now in these tags can be anything

If the tag name does not fit the directory name, I must prepare it to make it suitable by replacing these characters with something suitable. Thus, the creation of the directory is not interrupted. I used the following code, but realized that was not enough.

path = path.replace("/","-"); path = path.replace("\\","-"); 

Please advise that this is the best way to do this.

thanks,

+6
source share
4 answers

Here you can use the full list of invalid characters to handle the replacement as you like. They are available directly through Path.GetInvalidFileNameChars and Path.GetInvalidPathChars .

+4
source

Import the System.IO namespace and use the path

 Path.GetInvalidPathChars 

and for the file name use

 Path.GetInvalidFileNameChars 

For eg

 string filename = "salmnas dlajhdla kjha;dmas'lkasn"; foreach (char c in Path.GetInvalidFileNameChars()) filename = filename.Replace(System.Char.ToString(c), ""); foreach (char c in Path.GetInvalidPathChars()) filename = filename.Replace(System.Char.ToString(c), ""); 

Then u can use Path.Combine to add tags to create the path.

 string mypath = Path.Combine(@"C:\", "First_Tag", "Second_Tag"); //return C:\First_Tag\Second_Tag 
+5
source

You should now use the following characters ? < > | : \ / * " ? < > | : \ / * "

  string PathFix(string path) { List<string> _forbiddenChars = new List<string>(); _forbiddenChars.Add("?"); _forbiddenChars.Add("<"); _forbiddenChars.Add(">"); _forbiddenChars.Add(":"); _forbiddenChars.Add("|"); _forbiddenChars.Add("\\"); _forbiddenChars.Add("/"); _forbiddenChars.Add("*"); _forbiddenChars.Add("\""); for (int i = 0; i < _forbiddenChars.Count; i++) { path = path.Replace(_forbiddenChars[i], ""); } return path; } 

Tip. You cannot include double quotation mark ( " ), but you can include 2 quotation mark ( '' ). In this case:

  string PathFix(string path) { List<string> _forbiddenChars = new List<string>(); _forbiddenChars.Add("?"); _forbiddenChars.Add("<"); _forbiddenChars.Add(">"); _forbiddenChars.Add(":"); _forbiddenChars.Add("|"); _forbiddenChars.Add("\\"); _forbiddenChars.Add("/"); _forbiddenChars.Add("*"); //_forbiddenChars.Add("\""); Do not delete the double-quote character, so we could replace it with 2 quotes (before the return). for (int i = 0; i < _forbiddenChars.Count; i++) { path = path.Replace(_forbiddenChars[i], ""); } path = path.Replace("\"", "''"); //Replacement here return path; } 

Of course, you will use only one of them (or combine them with one function with the bool parameter to replace the quote, if necessary)

+2
source

Correct answer Nikhil Agrawal has some syntax errors.

For reference only, here is the compilation version:

 public static string MakeValidFolderNameSimple(string folderName) { if (string.IsNullOrEmpty(folderName)) return folderName; foreach (var c in System.IO.Path.GetInvalidFileNameChars()) folderName = folderName.Replace(c.ToString(), string.Empty); foreach (var c in System.IO.Path.GetInvalidPathChars()) folderName = folderName.Replace(c.ToString(), string.Empty); return folderName; } 
+1
source

Source: https://habr.com/ru/post/912807/


All Articles