Gar*_*hby 423 c# string directory path
我需要一种强大而简单的方法来从简单的字符串中删除非法路径和文件字符.我使用了下面的代码,但它似乎没有做任何事情,我错过了什么?
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string illegal = "\"M<>\"\\a/ry/ h**ad:>> a\\/:*?\"<>| li*tt|le|| la\"mb.?";
illegal = illegal.Trim(Path.GetInvalidFileNameChars());
illegal = illegal.Trim(Path.GetInvalidPathChars());
Console.WriteLine(illegal);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*ley 473
尝试这样的事情;
string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
foreach (char c in invalid)
{
illegal = illegal.Replace(c.ToString(), "");
}
Run Code Online (Sandbox Code Playgroud)
但我不得不同意这些评论,我可能会尝试处理非法路径的来源,而不是试图将非法路径变成合法但可能是非预期路径.
编辑:或使用正则表达式的潜在"更好"的解决方案.
string illegal = "\"M\"\\a/ry/ h**ad:>> a\\/:*?\"| li*tt|le|| la\"mb.?";
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
illegal = r.Replace(illegal, "");
Run Code Online (Sandbox Code Playgroud)
不过,问题还有待提出,为什么你首先要这样做.
She*_*wzy 298
public string RemoveInvalidChars(string filename)
{
return string.Concat(filename.Split(Path.GetInvalidFileNameChars()));
}
Run Code Online (Sandbox Code Playgroud)
这个答案是Ceres的另一个主题,我真的很喜欢它简洁明了.
Mic*_*ton 206
我使用Linq来清理文件名.您可以轻松扩展它以检查有效路径.
private static string CleanFileName(string fileName)
{
return Path.GetInvalidFileNameChars().Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
}
Run Code Online (Sandbox Code Playgroud)
一些评论表明这种方法对他们不起作用所以我已经包含了一个指向DotNetFiddle代码段的链接,因此您可以验证该方法.
https://dotnetfiddle.net/nw1SWY
Gre*_*vec 88
您可以使用Linq删除非法字符,如下所示:
var invalidChars = Path.GetInvalidFileNameChars();
var invalidCharsRemoved = stringWithInvalidChars
.Where(x => !invalidChars.Contains(x))
.ToArray();
Run Code Online (Sandbox Code Playgroud)
编辑
这是注释中提到的所需编辑的外观:
var invalidChars = Path.GetInvalidFileNameChars();
string invalidCharsRemoved = new string(stringWithInvalidChars
.Where(x => !invalidChars.Contains(x))
.ToArray());
Run Code Online (Sandbox Code Playgroud)
Ren*_*ené 27
这些都是很好的解决方案,但它们都依赖于Path.GetInvalidFileNameChars,这可能不像你想象的那么可靠.请注意MSDN文档中的以下注释Path.GetInvalidFileNameChars:
从此方法返回的数组不保证包含在文件和目录名称中无效的完整字符集.完整的无效字符集可能因文件系统而异.例如,在基于Windows的桌面平台上,无效路径字符可能包括ASCII/Unicode字符1到31,以及quote("),小于(<),大于(>),pipe(|),退格(\b),null(\ 0)和制表符(\ t).
方法并没有更好Path.GetInvalidPathChars.它包含完全相同的注释.
Lil*_*ley 23
对于文件名:
string cleanFileName = String.Join("", fileName.Split(Path.GetInvalidFileNameChars()));
Run Code Online (Sandbox Code Playgroud)
对于完整路径:
string cleanPath = String.Join("", path.Split(Path.GetInvalidPathChars()));
Run Code Online (Sandbox Code Playgroud)
use*_*116 18
对于初学者,Trim仅从字符串的开头或结尾删除字符.其次,您应该评估是否确实要删除令人反感的字符,或者快速失败并让用户知道他们的文件名无效.我的选择是后者,但我的回答至少应该告诉你如何以正确和错误的方式做事:
StackOverflow问题显示如何检查给定字符串是否是有效的文件名.请注意,您可以使用此问题的正则表达式删除带有正则表达式替换的字符(如果您确实需要这样做).
Jef*_*tes 16
我使用正则表达式来实现这一点.首先,我动态构建正则表达式.
string regex = string.Format(
"[{0}]",
Regex.Escape(new string(Path.GetInvalidFileNameChars())));
Regex removeInvalidChars = new Regex(regex, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant);
Run Code Online (Sandbox Code Playgroud)
然后我只需调用removeInvalidChars.Replace来执行查找和替换.这显然可以扩展到覆盖路径字符.
ano*_*ani 15
从用户输入中删除非法字符的最佳方法是使用Regex类替换非法字符,在代码后面创建方法,或者使用RegularExpression控件在客户端验证.
public string RemoveSpecialCharacters(string str)
{
return Regex.Replace(str, "[^a-zA-Z0-9_]+", "_", RegexOptions.Compiled);
}
Run Code Online (Sandbox Code Playgroud)
要么
<asp:RegularExpressionValidator ID="regxFolderName"
runat="server"
ErrorMessage="Enter folder name with a-z A-Z0-9_"
ControlToValidate="txtFolderName"
Display="Dynamic"
ValidationExpression="^[a-zA-Z0-9_]*$"
ForeColor="Red">
Run Code Online (Sandbox Code Playgroud)
Jan*_*Jan 14
我绝对更喜欢杰夫耶茨的想法.如果你稍微修改它,它将完美地工作:
string regex = String.Format("[{0}]", Regex.Escape(new string(Path.GetInvalidFileNameChars())));
Regex removeInvalidChars = new Regex(regex, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.CultureInvariant);
Run Code Online (Sandbox Code Playgroud)
改进只是为了逃避自动生成的正则表达式.
小智 11
这是一个应该有助于.NET 3及更高版本的代码片段.
using System.IO;
using System.Text.RegularExpressions;
public static class PathValidation
{
private static string pathValidatorExpression = "^[^" + string.Join("", Array.ConvertAll(Path.GetInvalidPathChars(), x => Regex.Escape(x.ToString()))) + "]+$";
private static Regex pathValidator = new Regex(pathValidatorExpression, RegexOptions.Compiled);
private static string fileNameValidatorExpression = "^[^" + string.Join("", Array.ConvertAll(Path.GetInvalidFileNameChars(), x => Regex.Escape(x.ToString()))) + "]+$";
private static Regex fileNameValidator = new Regex(fileNameValidatorExpression, RegexOptions.Compiled);
private static string pathCleanerExpression = "[" + string.Join("", Array.ConvertAll(Path.GetInvalidPathChars(), x => Regex.Escape(x.ToString()))) + "]";
private static Regex pathCleaner = new Regex(pathCleanerExpression, RegexOptions.Compiled);
private static string fileNameCleanerExpression = "[" + string.Join("", Array.ConvertAll(Path.GetInvalidFileNameChars(), x => Regex.Escape(x.ToString()))) + "]";
private static Regex fileNameCleaner = new Regex(fileNameCleanerExpression, RegexOptions.Compiled);
public static bool ValidatePath(string path)
{
return pathValidator.IsMatch(path);
}
public static bool ValidateFileName(string fileName)
{
return fileNameValidator.IsMatch(fileName);
}
public static string CleanPath(string path)
{
return pathCleaner.Replace(path, "");
}
public static string CleanFileName(string fileName)
{
return fileNameCleaner.Replace(fileName, "");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
上面的大多数解决方案将路径和文件名的非法字符组合在一起是错误的(即使两个调用当前都返回相同的字符集).我首先在路径和文件名中拆分路径+文件名,然后将相应的设置应用于它们,然后再将两者合并.
wvd_vegt
如果删除或替换单个字符的无效字符,则可能发生冲突:
<abc -> abc
>abc -> abc
Run Code Online (Sandbox Code Playgroud)
这是一个避免这种情况的简单方法:
public static string ReplaceInvalidFileNameChars(string s)
{
char[] invalidFileNameChars = System.IO.Path.GetInvalidFileNameChars();
foreach (char c in invalidFileNameChars)
s = s.Replace(c.ToString(), "[" + Array.IndexOf(invalidFileNameChars, c) + "]");
return s;
}
Run Code Online (Sandbox Code Playgroud)
结果:
<abc -> [1]abc
>abc -> [2]abc
Run Code Online (Sandbox Code Playgroud)
这似乎是 O(n) 并且不会在字符串上花费太多内存:
private static readonly HashSet<char> invalidFileNameChars = new HashSet<char>(Path.GetInvalidFileNameChars());
public static string RemoveInvalidFileNameChars(string name)
{
if (!name.Any(c => invalidFileNameChars.Contains(c))) {
return name;
}
return new string(name.Where(c => !invalidFileNameChars.Contains(c)).ToArray());
}
Run Code Online (Sandbox Code Playgroud)
抛出一个例外.
if ( fileName.IndexOfAny(Path.GetInvalidFileNameChars()) > -1 )
{
throw new ArgumentException();
}
Run Code Online (Sandbox Code Playgroud)
文件名不能包含Path.GetInvalidPathChars()、+、#等特殊名称的字符。我们将所有检查合并为一类:
public static class FileNameExtensions
{
private static readonly Lazy<string[]> InvalidFileNameChars =
new Lazy<string[]>(() => Path.GetInvalidPathChars()
.Union(Path.GetInvalidFileNameChars()
.Union(new[] { '+', '#' })).Select(c => c.ToString(CultureInfo.InvariantCulture)).ToArray());
private static readonly HashSet<string> ProhibitedNames = new HashSet<string>
{
@"aux",
@"con",
@"clock$",
@"nul",
@"prn",
@"com1",
@"com2",
@"com3",
@"com4",
@"com5",
@"com6",
@"com7",
@"com8",
@"com9",
@"lpt1",
@"lpt2",
@"lpt3",
@"lpt4",
@"lpt5",
@"lpt6",
@"lpt7",
@"lpt8",
@"lpt9"
};
public static bool IsValidFileName(string fileName)
{
return !string.IsNullOrWhiteSpace(fileName)
&& fileName.All(o => !IsInvalidFileNameChar(o))
&& !IsProhibitedName(fileName);
}
public static bool IsProhibitedName(string fileName)
{
return ProhibitedNames.Contains(fileName.ToLower(CultureInfo.InvariantCulture));
}
private static string ReplaceInvalidFileNameSymbols([CanBeNull] this string value, string replacementValue)
{
if (value == null)
{
return null;
}
return InvalidFileNameChars.Value.Aggregate(new StringBuilder(value),
(sb, currentChar) => sb.Replace(currentChar, replacementValue)).ToString();
}
public static bool IsInvalidFileNameChar(char value)
{
return InvalidFileNameChars.Value.Contains(value.ToString(CultureInfo.InvariantCulture));
}
public static string GetValidFileName([NotNull] this string value)
{
return GetValidFileName(value, @"_");
}
public static string GetValidFileName([NotNull] this string value, string replacementValue)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException(@"value should be non empty", nameof(value));
}
if (IsProhibitedName(value))
{
return (string.IsNullOrWhiteSpace(replacementValue) ? @"_" : replacementValue) + value;
}
return ReplaceInvalidFileNameSymbols(value, replacementValue);
}
public static string GetFileNameError(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
return CommonResources.SelectReportNameError;
}
if (IsProhibitedName(fileName))
{
return CommonResources.FileNameIsProhibited;
}
var invalidChars = fileName.Where(IsInvalidFileNameChar).Distinct().ToArray();
if(invalidChars.Length > 0)
{
return string.Format(CultureInfo.CurrentCulture,
invalidChars.Length == 1 ? CommonResources.InvalidCharacter : CommonResources.InvalidCharacters,
StringExtensions.JoinQuoted(@",", @"'", invalidChars.Select(c => c.ToString(CultureInfo.CurrentCulture))));
}
return string.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
方法GetValidFileName将所有不正确的数据替换为_.
如果您必须在项目中的许多地方使用该方法,您还可以创建一个扩展方法并在项目中的任何位置调用它以获取字符串。
public static class StringExtension
{
public static string RemoveInvalidChars(this string originalString)
{
string finalString=string.Empty;
if (!string.IsNullOrEmpty(originalString))
{
return string.Concat(originalString.Split(Path.GetInvalidFileNameChars()));
}
return finalString;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以将上述扩展方法调用为:
string illegal = "\"M<>\"\\a/ry/ h**ad:>> a\\/:*?\"<>| li*tt|le|| la\"mb.?";
string afterIllegalChars = illegal.RemoveInvalidChars();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
408218 次 |
| 最近记录: |