如何检查路径中的非法字符? [英] How do I check for illegal characters in a path?

查看:612
本文介绍了如何检查路径中的非法字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.Net中,是否有办法检查用于路径的字符串是否包含无效字符?我知道我可以遍历Path.InvalidPathChars中的每个字符,以查看我的String是否包含一个字符,但是我更喜欢一个简单的,也许是更正式的解决方案.

Is there a way to check if a String meant for a path has invalid characters, in .Net? I know I could iterate over each character in Path.InvalidPathChars to see if my String contained one, but I'd prefer a simple, perhaps more formal, solution.

有一个吗?

如果只检查Get

更新:

我发现GetInvalidPathChars不能覆盖所有无效的路径字符. GetInvalidFileNameChars还有5个,包括?",我遇到过.我将切换到该位置,并且如果事实证明它也不足够,我也会报告.

I've found GetInvalidPathChars does not cover every invalid path character. GetInvalidFileNameChars has 5 more, including '?', which I've come across. I'm going to switch to that, and I'll report back if it, too, proves to be inadequate.

更新2:

GetInvalidFileNameChars绝对不是我想要的.它包含:",任何绝对路径都将包含:".我想我毕竟将只需要使用GetInvalidPathChars,并添加?"以及其他任何会在出现问题时引起我麻烦的角色.欢迎提供更好的解决方案.

GetInvalidFileNameChars is definitely not what I want. It contains ':', which any absolute path is going to contain ("C:\whatever"). I think I'm just going to have to use GetInvalidPathChars after all, and add in '?' and any other characters that cause me problems as they come up. Better solutions welcome.

推荐答案

InvalidPathChars已弃用.改用GetInvalidPathChars():

InvalidPathChars is deprecated. Use GetInvalidPathChars() instead:

    public static bool FilePathHasInvalidChars(string path)
    {

        return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
    }

稍长一些,但是在一个函数中处理路径vs文件无效字符:

Slightly longer, but handles path vs file invalid chars in one function:

    // WARNING: Not tested
    public static bool FilePathHasInvalidChars(string path)
    {
        bool ret = false;
        if(!string.IsNullOrEmpty(path))
        {
            try
            {
                // Careful!
                //    Path.GetDirectoryName("C:\Directory\SubDirectory")
                //    returns "C:\Directory", which may not be what you want in
                //    this case. You may need to explicitly add a trailing \
                //    if path is a directory and not a file path. As written, 
                //    this function just assumes path is a file path.
                string fileName = System.IO.Path.GetFileName(path);
                string fileDirectory = System.IO.Path.GetDirectoryName(path);

                // we don't need to do anything else,
                                    // if we got here without throwing an 
                                    // exception, then the path does not
                                    // contain invalid characters
            }
            catch (ArgumentException)
            {
                                    // Path functions will throw this 
                                    // if path contains invalid chars
                ret = true;
            }
        }
        return ret;
    }

这篇关于如何检查路径中的非法字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆