从文件名C#删除无效字符 [英] C# Remove Invalid Characters from Filename

查看:261
本文介绍了从文件名C#删除无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据通过EF3.5从SQL Server数据库的一个nvarchar领域来了。此字符串用来创建一个文件名,并需要删除无效字符,并试图下面的选项,但他们都不工作。请说明为​​什么这是这样一个可以理解的奥秘?难道我做错了什么?



我去了,虽然几乎所有的相关问题在这个网站..现在所有的建议/其他类似问题的答案张贴统一的问题。



UPD:这个问题是unrelated..All这些选项做的工作。因此,它发布到社区维基。

 公共静态字符串CleanFileName1(字符串文件名)
{
字符串的文件=文件名;
档= string.Concat(file.Split(System.IO.Path.GetInvalidFileNameChars(),StringSplitOptions.RemoveEmptyEntries));

如果(file.Length> 250)
{
档= file.Substring(0,250);
}
返回文件;
}

公共静态字符串CleanFileName2(字符串文件名)
{
变种建设者=新的StringBuilder();
无功无效= System.IO.Path.GetInvalidFileNameChars();
的foreach(在文件名VAR CUR)
{
如果
{
builder.Append(现)(invalid.Contains(CUR)!);
}
}
返回builder.ToString();
}

公共静态字符串CleanFileName3(字符串文件名)
{
串regexSearch =的String.Format({0} {1},
新的字符串(System.IO.Path.GetInvalidFileNameChars()),
新的字符串(System.IO.Path.GetInvalidPathChars()));
正则表达式R =新的正则表达式(的String.Format([{0}],Regex.Escape(regexSearch)));
字符串文件= r.Replace(文件名,);

返回文件;
}

公共静态字符串CleanFileName4(字符串文件名)
{
返回新的String(filename.Except(System.IO.Path.GetInvalidFileNameChars())。ToArray的());
}

公共静态字符串CleanFileName5(字符串文件名)
{
字符串文件=文件名;

的foreach(在System.IO.Path.GetInvalidFileNameChars字符C())
{
档= file.Replace(C,_);
}
返回文件;
}


解决方案

按System.IO.Path.GetInvalidFileNameChars()返回任何无效字符被删除。 - Bhuvan 5分钟前




您发布的作品确定为 Path.GetInvalidFileNameChars人物第一种方法() ,在这里它是在工作中:

 静态无效的主要(字串[] args)
{
列输入=ABC<&高清GT; ghi\\1234 / 5678 | 9:* 0;

字符串输出= CleanFileName1(输入);

Console.WriteLine(输出); //这个打印:abcdefghi1234567890

Console.Read();
}



我想虽然那你的问题是与某些特定于语言的特殊字符。您可以尝试在你的字符串打印出来的字符的ASCII码来解决此问题:

 字符串stringFromDatabase =/ 5678 | 9:* 0; //在这里从数据库中

的foreach得到它(CHAR℃的stringFromDatabase.ToCharArray())
Console.WriteLine((INT)C);

和咨询ASCII表:的 http://www.asciitable.com/



我的再次的犯罪嫌疑人,你会看到字符超过128个大码,你应该排除那些从你的字符串。


I have data coming from an nvarchar field of the SQL server database via EF3.5. This string is used to create a Filename and need to remove invalid characters and tried following options but none of them works. Please suggest why this is such an understandable mystery? Am I doing anything wrong?

I went though almost all of the related questions on this site.. and now posting a consolidated question from all the suggestions/answers from other similar questions.

UPD: The Issue was unrelated..All of these options do work. So posting it to community wiki.

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

解决方案

no invalid chars returned by System.IO.Path.GetInvalidFileNameChars() being removed. – Bhuvan 5 mins ago

The first method you posted works OK for the characters in Path.GetInvalidFileNameChars(), here it is at work:

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

I suppose though that your problem is with some language-specific special characters. You can try to troubleshoot this problem by printing out the ASCII codes of the characters in your string:

string stringFromDatabase = "/5678|?9:*0"; // here you get it from the database

foreach (char c in stringFromDatabase.ToCharArray())
    Console.WriteLine((int)c);

and consulting the ASCII table: http://www.asciitable.com/

I again suspect that you'll see characters with codes larger than 128, and you should exclude those from your string.

这篇关于从文件名C#删除无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆