Jon*_*len 163
呃,当人们试图猜测哪些角色有效时,我讨厌它.除了完全不可移植(总是考虑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
Run Code Online (Sandbox Code Playgroud)
Squ*_*rel 32
要删除无效字符:
static readonly char[] invalidFileNameChars = Path.GetInvalidFileNameChars();
// Builds a string out of valid chars
var validFilename = new string(filename.Where(ch => !invalidFileNameChars.Contains(ch)).ToArray());
Run Code Online (Sandbox Code Playgroud)
要替换无效字符:
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());
Run Code Online (Sandbox Code Playgroud)
要替换无效字符(并避免潜在的名称冲突,如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());
Run Code Online (Sandbox Code Playgroud)
Dou*_*rch 32
这个问题已经被问很多 次 之前,正如之前多次指出,IO.Path.GetInvalidFileNameChars是不够的.
首先,有许多名称,如PRN和CON,这些名称是保留的,不允许用于文件名.还有其他名称不允许仅在根文件夹中.也不允许以句点结尾的名称.
其次,存在各种长度限制.在这里阅读NTFS的完整列表.
第三,您可以附加到具有其他限制的文件系统.例如,ISO 9660文件名不能以" - "开头,但可以包含它.
第四,如果两个进程"任意"选择相同的名称,你会怎么做?
通常,使用外部生成的文件名名称是个坏主意.我建议生成您自己的私有文件名并在内部存储人类可读的名称.
Aar*_*ner 22
我同意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));
Run Code Online (Sandbox Code Playgroud)
ps - 这比它应该更加神秘 - 我试图简洁.
cse*_*lls 13
这是我的版本:
static string GetSafeFileName(string name, char replace = '_') {
char[] invalids = Path.GetInvalidFileNameChars();
return new string(name.Select(c => invalids.Contains(c) ? replace : c).ToArray());
}
Run Code Online (Sandbox Code Playgroud)
我不确定如何计算GetInvalidFileNameChars的结果,但"Get"表明它是非平凡的,所以我缓存了结果.此外,这只会遍历输入字符串一次而不是多次,就像上面的迭代遍历无效字符集的解决方案一样,在源字符串中一次替换它们.此外,我喜欢基于位置的解决方案,但我更喜欢替换无效的字符而不是删除它们.最后,我的替换只是一个字符,以避免在迭代字符串时将字符转换为字符串.
我说所有这些都没有进行分析 - 这个对我来说"感觉"很好.:)
sid*_*guy 11
这是我现在使用的功能(感谢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;
}
Run Code Online (Sandbox Code Playgroud)
为了方便起见,我把它放在"助手"课程中.
如果你想快速删除所有特殊字符,这些特殊字符有时候用户可以读取文件名,这很有效:
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"
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我刚刚添加到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)
}
Run Code Online (Sandbox Code Playgroud)
小智 5
为什么不将字符串转换为像这样的Base64等效项:
string UnsafeFileName = "salmnas dlajhdla kjha;dmas'lkasn";
string SafeFileName = Convert.ToBase64String(Encoding.UTF8.GetBytes(UnsafeFileName));
Run Code Online (Sandbox Code Playgroud)
如果您想将其转换回去,可以阅读以下内容:
UnsafeFileName = Encoding.UTF8.GetString(Convert.FromBase64String(SafeFileName));
Run Code Online (Sandbox Code Playgroud)
我用它来保存来自随机描述的具有唯一名称的PNG文件。
| 归档时间: |
|
| 查看次数: |
60317 次 |
| 最近记录: |