2017-09-19 100 views
0

我没看到下面的代码有什么问题。在mscorlib.dll 'System.ArgumentException'c#路径中的非法字符

其他信息:它在第4行

引发错误
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); 
} 

抛出异常路径中具有非法字符。

+0

什么是'ConfigurationSettings.AppSettings [ “RapServer”]的内容;'和'ConfigurationSettings.AppSettings [ “importDir”];'? – SeM

+0

星号不是ReadAllText使用的文件名中的有效字符,它是用于匹配多个文件的通配符如果要读取多个文件,则需要使用目录方法来获取匹配文件的列表并将其读取一个一个。 – PaulF

+2

单个文件名可能不包含“*” –

回答

4

您可以随时检查您的路径,看它是否包含无效的路径字符。 尝试使用方法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

相关问题