【问题标题】:C# Remove Invalid Characters from FilenameC#从文件名中删除无效字符
【发布时间】:2011-04-19 00:48:36
【问题描述】:

我的数据来自通过 EF3.5 的 SQL 服务器数据库的 nvarchar 字段。此字符串用于创建文件名,需要删除无效字符并尝试以下选项,但它们都不起作用。请提出为什么这是一个可以理解的谜团?我做错什么了吗?

我浏览了该网站上几乎所有相关问题.. 现在发布一个综合问题,来自其他类似问题的所有建议/答案。

UPD:问题无关。所有这些选项都有效。所以把它发布到社区维基。

public static string CleanFileName1(string filename)
{            
    string file = filename;                                            
    file = string.Concat(file.Split(System.IO.Path.GetInvalidFileNameChars(), StringSplitOptions.RemoveEmptyEntries));

    if (file.Length > 250)
    {
        file = file.Substring(0, 250);
    }
    return file;
 }

public static string CleanFileName2(string filename)
{
    var builder = new StringBuilder();
    var invalid = System.IO.Path.GetInvalidFileNameChars();
    foreach (var cur in filename)
    {
        if (!invalid.Contains(cur))
        {
            builder.Append(cur);
        }
    }
    return builder.ToString();
}

public static string CleanFileName3(string filename)
{                                    
    string regexSearch = string.Format("{0}{1}",
        new string(System.IO.Path.GetInvalidFileNameChars()),
        new string(System.IO.Path.GetInvalidPathChars()));
    Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
    string file = r.Replace(filename, "");

    return file;
}       

public static string CleanFileName4(string filename)
{
    return new String(filename.Except(System.IO.Path.GetInvalidFileNameChars()).ToArray());
}   

public static string CleanFileName5(string filename)
{            
    string file = filename;

    foreach (char c in System.IO.Path.GetInvalidFileNameChars())
    {
        file = file.Replace(c, '_');
    }                                 
    return file;
}   

【问题讨论】:

  • “它们都不起作用”是什么意思?
  • 返回的字符串仍然包含无效字符。没有删除 System.IO.Path.GetInvalidFileNameChars() 返回的无效字符。
  • 如果您向我们展示一个无效输入的示例,这将得到最好的解释。
  • 上述所有函数都适用于来自数据库的 char 或 varchar 字段的字符串。但不适用于来自 nvarchar 字段的字符串。和编码有关吗?
  • @Dan .. 字符串按原样返回...没有使用上述任何函数删除任何字符。

标签: c# string


【解决方案1】:

这是我在静态公共类中使用的一个函数:

public static string RemoveInvalidFilePathCharacters(string filename, string replaceChar)
{
    string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
    Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
    return r.Replace(filename, replaceChar);
}

【讨论】:

    【解决方案2】:

    System.IO.Path.GetInvalidFileNameChars() 返回的无效字符没有被删除。 – Bhuvan 5 分钟前

    您发布的第一种方法适用于Path.GetInvalidFileNameChars() 中的字符,它正在起作用:

    static void Main(string[] args)
    {
        string input = "abc<def>ghi\\1234/5678|?9:*0";
    
        string output = CleanFileName1(input);
    
        Console.WriteLine(output); // this prints: abcdefghi1234567890
    
        Console.Read();
    }
    

    我想您的问题出在某些特定于语言的特殊字符上。您可以尝试通过打印字符串中字符的 ASCII 码来解决此问题:

    string stringFromDatabase = "/5678|?9:*0"; // here you get it from the database
    
    foreach (char c in stringFromDatabase.ToCharArray())
        Console.WriteLine((int)c);
    

    并查阅 ASCII 表:http://www.asciitable.com/

    再次怀疑您会看到代码大于 128 的字符,您应该从字符串中排除这些字符。

    【讨论】:

    • 这适用于这样的普通字符串,但不是字符串来自数据库的 nvarchar 字段。
    • 您可以复制并粘贴您从数据库中收到的字符串作为评论吗?
    • "fbo test Investor 12/30/92" 在这个字符串中,我试图删除 / 并没有删除它们。但是当我尝试同样的事情时......从即时窗口......通过粘贴字符串。它会删除那些字符。
    • @Bhuvan - 在来自数据库的字符串中,您可能有其他看不到的字符...尝试打印字符串中每个字符的 ASCII 码,如我所示在我的回答中,看看你得到了什么。
    • 如何从数据库中提取数据?您是否有机会以不同的方式格式化日期,以使“/”不在日期中?像 CONVERT(VarChar(50), GETDATE(), 102)
    【解决方案3】:

    试试这个

    filename = Regex.Replace(filename, "[\/?:*""&gt;&lt;|]+", "", RegexOptions.Compiled)

    【讨论】:

    • @DJ .. 这个问题也一样...适用于常规字符串,但不适用于来自数据库的 nvarchar 字段的字符串
    • filename = Regex.Replace(filename, @"[\/?:*""&gt;&lt;|]+", "", RegexOptions.Compiled);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2017-04-01
    • 2022-11-14
    • 2017-02-02
    相关资源
    最近更新 更多