C# 文件名清洗

211
最近我一直在将各种MP3从不同的位置移动到一个仓库中。我一直在使用ID3标签构建新的文件名(感谢TagLib-Sharp!),但我发现我会收到“System.NotSupportedException:给定路径的格式不受支持”的错误提示。这是由File.Copy()Directory.CreateDirectory()生成的。
很快我意识到我的文件名需要被过滤清理。所以我做了一个显而易见的事情:
public static string SanitizePath_(string path, char replaceChar)
{
    string dir = Path.GetDirectoryName(path);
    foreach (char c in Path.GetInvalidPathChars())
        dir = dir.Replace(c, replaceChar);

    string name = Path.GetFileName(path);
    foreach (char c in Path.GetInvalidFileNameChars())
        name = name.Replace(c, replaceChar);

    return dir + name;
}

令我惊讶的是,我仍然收到异常。原来,':'不在Path.GetInvalidPathChars()集合中,因为它在路径根目录中是有效的。我想这很有道理 - 但这必须是一个相当普遍的问题。是否有一些简短的代码可以清理路径?最彻底的代码是我能想出来的,但感觉可能有些过度了。
    // replaces invalid characters with replaceChar
    public static string SanitizePath(string path, char replaceChar)
    {
        // construct a list of characters that can't show up in filenames.
        // need to do this because ":" is not in InvalidPathChars
        if (_BadChars == null)
        {
            _BadChars = new List<char>(Path.GetInvalidFileNameChars());
            _BadChars.AddRange(Path.GetInvalidPathChars());
            _BadChars = Utility.GetUnique<char>(_BadChars);
        }

        // remove root
        string root = Path.GetPathRoot(path);
        path = path.Remove(0, root.Length);

        // split on the directory separator character. Need to do this
        // because the separator is not valid in a filename.
        List<string> parts = new List<string>(path.Split(new char[]{Path.DirectorySeparatorChar}));

        // check each part to make sure it is valid.
        for (int i = 0; i < parts.Count; i++)
        {
            string part = parts[i];
            foreach (char c in _BadChars)
            {
                part = part.Replace(c, replaceChar);
            }
            parts[i] = part;
        }

        return root + Utility.Join(parts, Path.DirectorySeparatorChar.ToString());
    }

任何能使这个函数更快、更简单的改进都将不胜感激。

14个回答

0
如果您将目录和文件名连接在一起并对其进行清理,代码会更加简洁。至于清理掉“:”,只需取字符串中的第二个字符。如果它等于“replacechar”,则用冒号替换它。由于此应用程序是供您自己使用的,这样的解决方案应该足够了。

0

对于 .NET7+ 项目,也可以使用生成的正则表达式扩展方法,如下所示:

public static class IOExtensions {
    [GeneratedRegex("CON|PRN|AUX|CLOCK\\$|NUL|COM0|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT0|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9", RegexOptions.Compiled | RegexOptions.IgnoreCase)]
    private static partial Regex GetReservedFilenamesRegex();

    public static string ToEscapedFilename(this string name, string replacer = "_") {
        return GetReservedFilenamesRegex().Replace(
            string.Join(
                replacer,
                name.Split(
                    Path.GetInvalidFileNameChars(),
                    StringSplitOptions.RemoveEmptyEntries
                )
            ),
            replacer
        );
    }
}

例如,
"Order * for AUX at 12/03/2023.csv".ToEscapedFileName()
将返回
"Order _ for _ at 12_03_2023.csv"

-1

基于@fiat和@Andre的方法,我也想分享我的解决方案。

  • 这是一个扩展方法
  • 正则表达式在第一次使用时编译,以节省大量执行时间
  • 保留了保留字
public static class StringPathExtensions
{
    private static Regex _invalidPathPartsRegex;
    
    static StringPathExtensions()
    {
        var invalidReg = System.Text.RegularExpressions.Regex.Escape(new string(Path.GetInvalidFileNameChars()));
        _invalidPathPartsRegex = new Regex($"(?<reserved>^(CON|PRN|AUX|CLOCK\\$|NUL|COM0|COM1|COM2|COM3|COM4|COM5|COM6|COM7|COM8|COM9|LPT0|LPT1|LPT2|LPT3|LPT4|LPT5|LPT6|LPT7|LPT8|LPT9))|(?<invalid>[{invalidReg}:]+|\\.$)", RegexOptions.Compiled);
    }

    public static string SanitizeFileName(this string path)
    {
        return _invalidPathPartsRegex.Replace(path, m =>
        {
            if (!string.IsNullOrWhiteSpace(m.Groups["reserved"].Value))
                return string.Concat("_", m.Groups["reserved"].Value);
            return "_";
        });
    }
}

-1
using System;
using System.IO;
using System.Linq;
using System.Text;

public class Program
{
    public static void Main()
    {
        try
        {
            var badString = "ABC\\DEF/GHI<JKL>MNO:PQR\"STU\tVWX|YZA*BCD?EFG";
            Console.WriteLine(badString);
            Console.WriteLine(SanitizeFileName(badString, '.'));
            Console.WriteLine(SanitizeFileName(badString));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

    private static string SanitizeFileName(string fileName, char? replacement = null)
    {
        if (fileName == null) { return null; }
        if (fileName.Length == 0) { return ""; }

        var sb = new StringBuilder();
        var badChars = Path.GetInvalidFileNameChars().ToList();

        foreach (var @char in fileName)
        {
            if (badChars.Contains(@char)) 
            {
                if (replacement.HasValue)
                {
                    sb.Append(replacement.Value);
                }
                continue; 
            }
            sb.Append(@char);
        }
        return sb.ToString();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接