最近,我一直在将一堆mp3从不同的位置转移到一个存储库中。我一直在使用ID3标记构造新文件名(感谢TagLib-Sharp!),我注意到我得到了一个系统。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());
    }

任何改进,使这个功能更快,更少巴洛克式的将是非常赞赏的。


当前回答

如果您将目录和文件名附加在一起并对其进行消毒,而不是分别对它们进行消毒,那么您的代码将更加干净。至于清除:,只需取字符串中的第二个字符。如果它等于"replacechar",用冒号替换它。由于这个应用程序是为您自己使用,这样的解决方案应该是完全足够的。

其他回答

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();
    }
}

我使用System.IO.Path.GetInvalidFileNameChars()方法来检查无效字符,我没有任何问题。

我正在使用以下代码:

foreach( char invalidchar in System.IO.Path.GetInvalidFileNameChars())
{
    filename = filename.Replace(invalidchar, '_');
}

我过去在这方面取得过成功。

漂亮,短,静态:-)

    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);
    }

我希望以某种方式保留字符,而不仅仅是简单地用下划线替换字符。

我想到的一种方法是用类似的字符替换这些字符(在我的情况下),不太可能被用作常规字符。所以我从无效字符列表中找到了类似字符。

下面是使用look-a-like进行编码和解码的函数。

这段代码不包括所有System.IO.Path.GetInvalidFileNameChars()字符的完整列表。因此,扩展或使用下划线替换任何剩余字符由您自己决定。

private static Dictionary<string, string> EncodeMapping()
{
    //-- Following characters are invalid for windows file and folder names.
    //-- \/:*?"<>|
    Dictionary<string, string> dic = new Dictionary<string, string>();
    dic.Add(@"\", "Ì"); // U+OOCC
    dic.Add("/", "Í"); // U+OOCD
    dic.Add(":", "¦"); // U+00A6
    dic.Add("*", "¤"); // U+00A4
    dic.Add("?", "¿"); // U+00BF
    dic.Add(@"""", "ˮ"); // U+02EE
    dic.Add("<", "«"); // U+00AB
    dic.Add(">", "»"); // U+00BB
    dic.Add("|", "│"); // U+2502
    return dic;
}

public static string Escape(string name)
{
    foreach (KeyValuePair<string, string> replace in EncodeMapping())
    {
        name = name.Replace(replace.Key, replace.Value);
    }

    //-- handle dot at the end
    if (name.EndsWith(".")) name = name.CropRight(1) + "°";

    return name;
}

public static string UnEscape(string name)
{
    foreach (KeyValuePair<string, string> replace in EncodeMapping())
    {
        name = name.Replace(replace.Value, replace.Key);
    }

    //-- handle dot at the end
    if (name.EndsWith("°")) name = name.CropRight(1) + ".";

    return name;
}

你可以选择自己喜欢的样子。我使用windows中的字符地图应用程序来选择我的%windir%\system32\charmap.exe

当我通过发现进行调整时,我将更新这段代码。

如果您将目录和文件名附加在一起并对其进行消毒,而不是分别对它们进行消毒,那么您的代码将更加干净。至于清除:,只需取字符串中的第二个字符。如果它等于"replacechar",用冒号替换它。由于这个应用程序是为您自己使用,这样的解决方案应该是完全足够的。