在 C# 中将我自己的非法字符插入到 Path.GetInvalidFileNameChars() 中

发布于 2024-10-09 10:24:52 字数 240 浏览 3 评论 0原文

如何扩展 Path.GetInvalidFileNameChars 以包含我自己的应用程序中非法的字符集?

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

如果我想添加“&”作为一个非法角色,我可以这样做吗?

How can I extend the Path.GetInvalidFileNameChars to include my own set of characters that is illegal in my application?

string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());

If I wanted to add the '&' as an illegal character, could I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无所的.畏惧 2024-10-16 10:24:52
typeof(Path).GetField("InvalidFileNameChars", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, new[] { 'o', 'v', 'e', 'r', '9', '0', '0', '0' });
typeof(Path).GetField("InvalidFileNameChars", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, new[] { 'o', 'v', 'e', 'r', '9', '0', '0', '0' });
影子的影子 2024-10-16 10:24:52

试试这个:

var invalid = Path.GetInvalidFileNameChars().Concat(new [] { '&' });

这将产生一个包含所有无效字符(包括您的字符)的 IEnumerable

这是一个完整的示例:

using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        // This is the sequence of characters
        var invalid = Path.GetInvalidFileNameChars().Concat(new[] { '&' });
        // If you want them as an array you can do this
        var invalid2 = invalid.ToArray();
        // If you want them as a string you can do this
        var invalid3 = new string(invalid.ToArray());
    }
}

Try this:

var invalid = Path.GetInvalidFileNameChars().Concat(new [] { '&' });

This will yeild an IEnumerable<char> with all invalid characters, including yours.

Here is a full example:

using System.IO;
using System.Linq;

class Program
{
    static void Main()
    {
        // This is the sequence of characters
        var invalid = Path.GetInvalidFileNameChars().Concat(new[] { '&' });
        // If you want them as an array you can do this
        var invalid2 = invalid.ToArray();
        // If you want them as a string you can do this
        var invalid3 = new string(invalid.ToArray());
    }
}
平定天下 2024-10-16 10:24:52

您无法修改现有函数,但可以编写一个包装函数,该函数返回 Path.GetInvalidFileNameChars() 和非法字符。

public static string GetInvalidFileNameChars() {
    return Path.GetInvalidFileNameChars().Concat(MY_INVALID_FILENAME_CHARS);
}

You can't modify an existing function, but you can write a wrapper function that returns Path.GetInvalidFileNameChars() and your illegal characters.

public static string GetInvalidFileNameChars() {
    return Path.GetInvalidFileNameChars().Concat(MY_INVALID_FILENAME_CHARS);
}
紅太極 2024-10-16 10:24:52

扩展方法是您最好的选择。

public static class Extensions
{
    public static char[] GetApplicationInvalidChars(this char[] input)
    {
        //Your list of invalid characters goes below.
        var invalidChars = new [] { '%', '#', 't' };
        return String.Concat(input, invalidChars).ToCharArray();
    }
}

然后按如下方式使用它:

string invalid = Path.GetInvalidFileNameChars().GetApplicationInvalidChars();

它将把无效字符连接到其中已有的字符。

An extension method is your best bet here.

public static class Extensions
{
    public static char[] GetApplicationInvalidChars(this char[] input)
    {
        //Your list of invalid characters goes below.
        var invalidChars = new [] { '%', '#', 't' };
        return String.Concat(input, invalidChars).ToCharArray();
    }
}

Then use it as follows:

string invalid = Path.GetInvalidFileNameChars().GetApplicationInvalidChars();

It will concatenate your invalid characters to what's already in there.

养猫人 2024-10-16 10:24:52

首先创建一个辅助类“SanitizeFileName.cs”

public class SanitizeFileName
{
   public static string ReplaceInvalidFileNameChars(string fileName, char? replacement = null)
   {
      if (fileName != null && fileName.Length != 0)
      {
         var sb = new StringBuilder();
         var badChars = new[] { ',', ' ', '^', '°' };
         var inValidChars = Path.GetInvalidFileNameChars().Concat(badChars).ToList();

         foreach (var @char in fileName)
         {
            if (inValidChars.Contains(@char))
            {
               if (replacement.HasValue)
               {
                  sb.Append(replacement.Value);
               }
               continue;
            }
            sb.Append(@char);
         }
         return sb.ToString();
      }
      return null;
   }
}

然后,像这样使用它:

var validFileName = SanitizeFileName.ReplaceInvalidFileNameChars(filename, '_');

在我的例子中,我必须清理 ac# 下载方法中响应标头中“内容沉积”上的“文件名”。

Response.AddHeader("Content-Disposition", "attachment;filename=" + validFileName);

First create a helper class "SanitizeFileName.cs"

public class SanitizeFileName
{
   public static string ReplaceInvalidFileNameChars(string fileName, char? replacement = null)
   {
      if (fileName != null && fileName.Length != 0)
      {
         var sb = new StringBuilder();
         var badChars = new[] { ',', ' ', '^', '°' };
         var inValidChars = Path.GetInvalidFileNameChars().Concat(badChars).ToList();

         foreach (var @char in fileName)
         {
            if (inValidChars.Contains(@char))
            {
               if (replacement.HasValue)
               {
                  sb.Append(replacement.Value);
               }
               continue;
            }
            sb.Append(@char);
         }
         return sb.ToString();
      }
      return null;
   }
}

Then, use it like this:

var validFileName = SanitizeFileName.ReplaceInvalidFileNameChars(filename, '_');

in my case, i had to clean up the "filename" on the "Content-Deposition" in Response Headers in a c# download method.

Response.AddHeader("Content-Disposition", "attachment;filename=" + validFileName);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文