在"路径"中获取无效字符

时间:2018-04-19 21:06:37

标签: c#

System.ArgumentException: invalid characters in the "path".

em System.IO.Path.CheckInvalidPathChars(String path,Boolean checkAdditional)    em System.IO.Path.GetFileName(String path)    em System.IO.StreamWriter.CreateFile(String path,Boolean append,Boolean checkHost)    em System.IO.StreamWriter..ctor(String path,Boolean append,Encoding encoding,Int32 bufferSize,Boolean checkHost)    em System.IO.StreamWriter..ctor(String path,Boolean append,Encoding encoding)    em System.IO.File.InternalAppendAllText(String path,String contents,Encoding encoding)    em System.IO.File.AppendAllText(String path,String contents)    em Loader.Program.Main(String [] args)na C:\ Users \ Muni \ source \ repos \ encrypt \ encrypt \ Program.cs:linha 44

第44行

string name = Environment.UserName;
string temp = "C:\\Users\\" + name + "\\AppData\\Local\\Temp\\setup1.bat";
string tempe = new WebClient().DownloadString("website/setup1.txt");
                    File.AppendAllText(tempe, temp);

1 个答案:

答案 0 :(得分:1)

你的File.AppendAllText论点是倒退的:

  

public static void AppendAllText(           字符串路径,           字符串内容       )

string temp = "C:\\Users\\" + name + "\\AppData\\Local\\Temp\\setup1.bat";
string tempe = new WebClient().DownloadString("website/setup1.txt");
                    File.AppendAllText(temp, tempe);

只要路径可访问,就可以正常工作。您不应该使用字符串连接来创建路径,请使用:

string temp = Path.Combine(Path.GetTempPath(), "setup1.bat");
string tempe = new WebClient().DownloadString("website/setup1.txt");
                    File.AppendAllText(temp, tempe);

相反...... Path.GetTempPath将返回当前用户的临时文件路径。 Path.Combine将路径与正确的路径分隔符组合在一起。

相关问题