C# Sanitize File Name

前端 未结 12 1438
谎友^
谎友^ 2020-12-04 06:06

I recently have been moving a bunch of MP3s from various locations into a repository. I had been constructing the new file names using the ID3 tags (thanks, TagLib-Sharp!),

12条回答
  •  攒了一身酷
    2020-12-04 06:33

    I have had success with this in the past.

    Nice, short and static :-)

        public static string returnSafeString(string s)
        {
            foreach (char character in Path.GetInvalidFileNameChars())
            {
                s = s.Replace(character.ToString(),string.Empty);
            }
    
            foreach (char character in Path.GetInvalidPathChars())
            {
                s = s.Replace(character.ToString(), string.Empty);
            }
    
            return (s);
        }
    

提交回复
热议问题