我的程序将从互联网上获取任意字符串并将其用于文件名。有没有一种简单的方法可以从这些字符串中删除坏字符,还是需要为此编写自定义函数?
答案 0 :(得分:161)
呃,当人们试图猜测哪些字符有效时,我讨厌它。除了完全不可移植(总是考虑Mono)之外,早期的两篇评论都错过了25个无效字符。
'Clean just a filename
Dim filename As String = "salmnas dlajhdla kjha;dmas'lkasn"
For Each c In IO.Path.GetInvalidFileNameChars
filename = filename.Replace(c, "")
Next
'See also IO.Path.GetInvalidPathChars
答案 1 :(得分:31)
此问题已被问及many times before,并且如前所述多次提及IO.Path.GetInvalidFileNameChars
是不够的。
首先,有许多名称,如PRN和CON,这些名称是保留的,不允许用于文件名。还有其他名称不允许仅在根文件夹中。也不允许以句点结尾的名称。
其次,存在各种长度限制。阅读NTFS here的完整列表。
第三,您可以附加到具有其他限制的文件系统。例如,ISO 9660文件名不能以“ - ”开头,但可以包含它。
第四,如果两个过程“任意”选择相同的名称,你会怎么做?
通常,使用外部生成的文件名名称是个坏主意。我建议在内部生成您自己的私有文件名并存储人类可读的名称。
答案 2 :(得分:28)
删除无效字符:
static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars
var validFilename = new string(filename.Where(ch => !invalidFileNameChars.Contains(ch)).ToArray());
替换无效字符:
static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars and an _ for invalid ones
var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? '_' : ch).ToArray());
要替换无效字符(并避免潜在的名称冲突,如Hell * vs Hell $):
static readonly IList<char> invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars and replaces invalid chars with a unique letter (Moves the Char into the letter range of unicode, starting at "A")
var validFilename = new string(filename.Select(ch => invalidFileNameChars.Contains(ch) ? Convert.ToChar(invalidFileNameChars.IndexOf(ch) + 65) : ch).ToArray());
答案 3 :(得分:21)
我同意Grauenwolf并强烈推荐Path.GetInvalidFileNameChars()
这是我的C#贡献:
string file = @"38?/.\}[+=n a882 a.a*/|n^%$ ad#(-))";
Array.ForEach(Path.GetInvalidFileNameChars(),
c => file = file.Replace(c.ToString(), String.Empty));
P.S。 - 这比它应该更加神秘 - 我试图简洁。
答案 4 :(得分:12)
这是我的版本:
static string GetSafeFileName(string name, char replace = '_') {
char[] invalids = Path.GetInvalidFileNameChars();
return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
}
我不确定如何计算GetInvalidFileNameChars的结果,但“Get”表明它非常重要,所以我缓存了结果。此外,这只会遍历输入字符串一次而不是多次,就像上面的迭代遍历无效字符集的解决方案一样,在源字符串中一次替换它们。此外,我喜欢基于位置的解决方案,但我更喜欢替换无效的字符而不是删除它们。最后,我的替换只是一个字符,以避免在迭代字符串时将字符转换为字符串。
我说所有这些都不做分析 - 这个对我来说“感觉”很好。 :)
答案 5 :(得分:10)
这是我现在使用的功能(感谢jcollum用于C#示例):
public static string MakeSafeFilename(string filename, char replaceChar)
{
foreach (char c in System.IO.Path.GetInvalidFileNameChars())
{
filename = filename.Replace(c, replaceChar);
}
return filename;
}
为方便起见,我把它放在“助手”课程中。
答案 6 :(得分:6)
如果你想快速删除所有特殊字符,这些特殊字符有时更易于用户阅读文件名,这很有效:
string myCrazyName = "q`w^e!r@t#y$u%i^o&p*a(s)d_f-g+h=j{k}l|z:x\"c<v>b?n[m]q\\w;e'r,t.y/u";
string safeName = Regex.Replace(
myCrazyName,
"\W", /*Matches any nonword character. Equivalent to '[^A-Za-z0-9_]'*/
"",
RegexOptions.IgnoreCase);
// safeName == "qwertyuiopasd_fghjklzxcvbnmqwertyu"
答案 7 :(得分:5)
static class Utils
{
public static string MakeFileSystemSafe(this string s)
{
return new string(s.Where(IsFileSystemSafe).ToArray());
}
public static bool IsFileSystemSafe(char c)
{
return !Path.GetInvalidFileNameChars().Contains(c);
}
}
答案 8 :(得分:5)
这是我刚刚添加到ClipFlair(http://github.com/Zoomicon/ClipFlair)StringExtensions静态类(Utils.Silverlight项目)的内容,基于从Dour High Arch上面发布的相关stackoverflow问题的链接收集的信息:
public static string ReplaceInvalidFileNameChars(this string s, string replacement = "")
{
return Regex.Replace(s,
"[" + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]",
replacement, //can even use a replacement string of any length
RegexOptions.IgnoreCase);
//not using System.IO.Path.InvalidPathChars (deprecated insecure API)
}
答案 9 :(得分:4)
为什么不将字符串转换为类似的Base64:
string UnsafeFileName = "salmnas dlajhdla kjha;dmas'lkasn";
string SafeFileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(UnsafeFileName));
如果您想将其转换回来,那么您可以阅读它:
UnsafeFileName = Encoding.UTF8.GetString(Convert.FromBase64String(SafeFileName));
我用它来保存带有随机描述的唯一名称的PNG文件。
答案 10 :(得分:2)
private void textBoxFileName_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = CheckFileNameSafeCharacters(e);
}
/// <summary>
/// This is a good function for making sure that a user who is naming a file uses proper characters
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
internal static bool CheckFileNameSafeCharacters(System.Windows.Forms.KeyPressEventArgs e)
{
if (e.KeyChar.Equals(24) ||
e.KeyChar.Equals(3) ||
e.KeyChar.Equals(22) ||
e.KeyChar.Equals(26) ||
e.KeyChar.Equals(25))//Control-X, C, V, Z and Y
return false;
if (e.KeyChar.Equals('\b'))//backspace
return false;
char[] charArray = Path.GetInvalidFileNameChars();
if (charArray.Contains(e.KeyChar))
return true;//Stop the character from being entered into the control since it is non-numerical
else
return false;
}
答案 11 :(得分:1)
我觉得使用它很容易理解:
<Extension()>
Public Function MakeSafeFileName(FileName As String) As String
Return FileName.Where(Function(x) Not IO.Path.GetInvalidFileNameChars.Contains(x)).ToArray
End Function
这是有效的,因为string
IEnumerable
为char
数组,并且string
构造函数字符串采用char
数组。
答案 12 :(得分:0)
许多人建议使用Path.GetInvalidFileNameChars()
,这对我来说似乎是一个不好的解决方案。我鼓励您使用白名单而不是黑名单,因为黑客总会找到最终绕过它的方法。
以下是您可以使用的代码示例:
string whitelist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
foreach (char c in filename)
{
if (!whitelist.Contains(c))
{
filename = filename.Replace(c, '-');
}
}
答案 13 :(得分:0)
从我的较早项目中,我找到了这个解决方案,该解决方案在过去两年中一直运行良好。我用“!”替换了非法字符,然后检查是否有双!!,请使用您自己的字符。
public string GetSafeFilename(string filename)
{
string res = string.Join("!", filename.Split(Path.GetInvalidFileNameChars()));
while (res.IndexOf("!!") >= 0)
res = res.Replace("!!", "!");
return res;
}