How can I sanitize a string for use as a filename?

后端 未结 9 773
说谎
说谎 2020-12-23 22:56

I\'ve got a routine that converts a file into a different format and saves it. The original datafiles were numbered, but my routine gives the output a filename based on an

9条回答
  •  囚心锁ツ
    2020-12-23 23:38

    I did this:

    // Initialized elsewhere...
    string folder;
    string name;
    var prepl = System.IO.Path.GetInvalidPathChars();
    var frepl = System.IO.Path.GetInvalidFileNameChars();
    foreach (var c in prepl)
    {
        folder = folder.Replace(c,'_');
        name = name.Replace(c, '_');
    }
    foreach (var c in frepl)
    {
        folder = folder.Replace(c, '_');
        name = name.Replace(c, '_');
    }
    

提交回复
热议问题