C# Sanitize File Name

前端 未结 12 1472
谎友^
谎友^ 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:49

    there are a lot of working solutions here. just for the sake of completeness, here's an approach that doesn't use regex, but uses LINQ:

    var invalids = Path.GetInvalidFileNameChars();
    filename = invalids.Aggregate(filename, (current, c) => current.Replace(c, '_'));
    

    Also, it's a very short solution ;)

提交回复
热议问题