【问题标题】:How to test for "The path is not of a legal form."如何测试“路径不是合法形式”。
【发布时间】:2014-12-11 18:49:26
【问题描述】:

我在尝试获取时收到一个路径不是合法形式错误

IO.Path.GetDirectoryName(myString)

...其中(在这种情况下)myString 是一个空字符串。

首先测试一个空字符串很容易。

但是有没有针对字符串的更全面的测试,让各种无效的myString可以避免这个错误?

【问题讨论】:

  • 也许这会有所帮助stackoverflow.com/questions/6198392/…
  • @user469104 是的,谢谢。有趣的是错误“路径不是合法形式”。 ...这就是我搜索的内容...没有出现在那个长期存在的问题中

标签: .net string file-io


【解决方案1】:

旧帖子,但这里有一个简单的 try catch 解决方案

    /// <summary>
    /// Return the directory information of the path. Return null if path is not in legal form.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string GetDirectoryName(string path)
    {
        try
        {
            return Path.GetDirectoryName(path);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多