代码之家  ›  专栏  ›  技术社区  ›  Iasha

路径中的c#IIllegal字符

  •  0
  • Iasha  · 技术社区  · 9 年前

    private void ProcessCsvsReplaceNullsWithSpaces()
    {
        string server = ConfigurationSettings.AppSettings["RapServer"];
        string importDir = ConfigurationSettings.AppSettings["importDir"];
        string fileName = server + @"\" + importDir + "\\EMIR_VU_E_*.csv";
        string replacenull = File.ReadAllText(fileName);
        replacenull = replacenull.Replace("null", "");
        File.WriteAllText(fileName, replacenull);
    }
    

    引发异常:“系统。mscorlib中的ArgumentException。dll

    其他信息:路径中存在非法字符。

    1 回复  |  直到 9 年前
        1
  •  4
  •   Matthias Burger    9 年前

    您可以随时检查路径,是否包含无效的路径字符。 Path.GetInvalidPathChars .

    See the documentation of msdn

    bool containsInvalidChars = (!string.IsNullOrEmpty(filepath) 
            && filepath.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
    

    如果这是真的,那么您的路径中有一个无效的字符

    正如其他一些人提到的:文件名不能包含 *

    您还需要检查文件名是否包含无效字符:

    方法如下: System.IO.Path.GetInvalidFileNameChars()

    :

    enter image description here