URLEncode 能否解决文件名中存在非法字符的问题 (C#)?

发布于 2024-07-26 05:34:22 字数 239 浏览 4 评论 0原文

我正在构建一个应用程序,它使用精心设计的 API 从 Youtube 获取数据,并且数据文件以相应视频的名称作为文件名保存。 然而,我的程序崩溃了,因为 YouTube 上的很多视频的标题中都含有在 Windows 下文件名中使用非法的字符。

对视频标题进行 URLEncoding 可以解决此问题吗?

如果是这样,这是最好的使用方法吗?实现 URLEncode 的最佳方法是什么?

谢谢! :)

I'm building an application that uses an elaborate API to fetch data from Youtube, and the data files are saved with the name of the corresponding video as the file name. However, my program is crashing because quite a lot of the videos on YouTube have characters in their titles that are illegal to use in file names under Windows.

Would URLEncoding the title of the video fix this problem?

If so, is this the best method to use, and what would be the best way to implement a URLEncode?

Thanks! :)

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

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

发布评论

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

评论(4

痴意少年 2024-08-02 05:34:22

如果你想进行 url 编码,你可以使用 HttpUtility.UrlEncode。 但我不确定我会不会。 它可能会删除您想要删除的所有字符,但它也会删除其他字符。

我想我可能会使用 路径。 GetInvalidFilenameChars 并用下划线替换名称中的任何无效内容。

当然,这不是可逆编码,但我认为它会产生更容易理解的文件名。 您可能想要创建一个索引文件,该文件也从原始标题映射到文件名。

Well if you want to do url encoding, you could use HttpUtility.UrlEncode. I'm not sure I would though. It may strip out all the characters you want it to, but it'll do others as well.

I think I'd probably use Path.GetInvalidFilenameChars and just replace anything invalid in the name with an underscore.

That's not a reversible encoding, of course, but I think it'll produce filenames which are easier to understand. You might want to create an index file which maps from original title to filename as well.

天邊彩虹 2024-08-02 05:34:22

Url Encoding 应该可以解决这个问题,因为它应该用“%”后跟一组十六进制替换任何无效的字符(以及一些有效的字符); 据我所知,这对于文件系统名称有效。

但这引出了两个问题:

  1. 能够清晰地读取文件名对用户来说重要吗? 如果没有,最好使用唯一的文件名(1.file、2.file、3.file)和从文件名 -> 的映射。 title

  2. 如果两个视频同名会怎样? 我认为这是第一个问题的延伸。

  3. 如果标题(url 编码时)比最大文件名长度长怎么办? 如果我没记错的话,NTFS 上文件名的最大长度是 255 个字符; 如果标题中的每个字符扩展为 3 个字符以进行 url 编码,则 85 个字符的标题可以满足 255 个字符的限制。

编辑/更新:UrlEncode 认为有些字符是无效的文件系统字符; 我特别遇到的是“\”。 所以,不,Url 编码并不安全。

Url Encoding should fix the problem, as it should replace any invalid char (and a few valid ones) with a '%' followed by a set of hex; to my knowledge that is valid for file system names.

This begs two questions though:

  1. Is being able to cleanly read a filename important for the user? If not, it might be better to use a unique file name (1.file, 2.file, 3.file) and a mapping from file name -> title

  2. What happens if two videos have the same name? Sort of an extension of the first question, I think.

  3. What if the title (when url encoded) is longer then the max filename length? If I recall correctly, max length for a filename is 255 characters on NTFS; if each char in a title expands to 3 chars for url encoding, then the 255 char limit could be met with an 85 char title.

EDIT/Update: There are some characters that UrlEncode considers valid which are invalid file system chars; the one I've specifically come across is '\'. So, no, Url Encoding would not be safe.

可是我不能没有你 2024-08-02 05:34:22

我最终遇到了类似的问题:

    static string Escape(string input)
    {
        StringBuilder builder = new StringBuilder(input.Length);
        for (int i = 0; i < input.Length; i++)
        {
            if (Path.GetInvalidPathChars().Contains(input[i]) || Path.GetInvalidFileNameChars().Contains(input[i]) || input[i] == '%')
            {
                builder.Append(Uri.HexEscape(input[i]));
            }
            else
            {
                builder.Append(input[i]);
            }
        }
        return builder.ToString();
    }

    static string Unescape(string input)
    {
        StringBuilder builder = new StringBuilder(input.Length);
        int index = 0;
        while (index < input.Length)
        {
            builder.Append(Uri.HexUnescape(input, ref index));
        }
        return builder.ToString();
    }

必须编写所有这些代码感觉有点奇怪,但至少我得到了可以安全地与操作系统一起使用的可读文件名。

I ended up doing this with a similar problem:

    static string Escape(string input)
    {
        StringBuilder builder = new StringBuilder(input.Length);
        for (int i = 0; i < input.Length; i++)
        {
            if (Path.GetInvalidPathChars().Contains(input[i]) || Path.GetInvalidFileNameChars().Contains(input[i]) || input[i] == '%')
            {
                builder.Append(Uri.HexEscape(input[i]));
            }
            else
            {
                builder.Append(input[i]);
            }
        }
        return builder.ToString();
    }

    static string Unescape(string input)
    {
        StringBuilder builder = new StringBuilder(input.Length);
        int index = 0;
        while (index < input.Length)
        {
            builder.Append(Uri.HexUnescape(input, ref index));
        }
        return builder.ToString();
    }

It felt a bit weird to have to write all this code, but at least I get readable file names that are safe to use with the OS.

生活了然无味 2024-08-02 05:34:22

您可以使用 youtube 的视频 ID 代替视频名称吗? 例如 v=Yk6oPsKZG_w。 或者您无权访问该内容? 这些似乎包含简单的字母数字,并且在 YouTube 中应该是唯一的。

我不确定 urlencode 是否可以帮助您处理视频名称中的星号。

如果您仍然想使用视频名称,您可能需要考虑使用“\\?\”前缀,它告诉 Win32 API 禁用所有字符串解析并将该字符串直接发送到文件系统。

http://msdn.microsoft.com/en -us/library/aa365247(VS.85).aspx#path_names_and_namespaces

我不确定您是否可以将其与.NET API 一起使用,或者是否必须使用 DllImport 直接调用 Win32 API。

Instead of the video name can you use youtube's video id? e.g. v=Yk6oPsKZG_w. Or do you not have access to that? Those seem to contain simple alphanumerics and should be unique within youtube.

I'm not sure if urlencode will help you with asterisks in the video name.

If you still want to use the video name you may want to look at using the "\\?\" prefix which tells the Win32 APIs to disable all string parsing and to send this string straight to the file system.

http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx#path_names_and_namespaces

I'm not sure, if you can use that with the .NET API or if you would have to use DllImport to invoke the Win32 API directly.

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