c#路径格式不支持WriteAllines

c#路径格式不支持WriteAllines,c#,winforms,C#,Winforms,我正在尝试为我的实习制作一个小的Windows窗体测验应用程序,我一直在把结果保存到一个保存文件中。这是我使用的代码: string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); System.IO.File.WriteAllLines(desktopPath + @"\saveFile_" + DateTime.Now.ToString() + ".txt", save

我正在尝试为我的实习制作一个小的Windows窗体测验应用程序,我一直在把结果保存到一个保存文件中。这是我使用的代码:

string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

System.IO.File.WriteAllLines(desktopPath + @"\saveFile_" + DateTime.Now.ToString() + ".txt", saveFile);
当我点击按钮保存到一个新的文本文件时,它崩溃并告诉我路径格式不受支持


如何更正它以将其保存到桌面上的新文本文件中?

DateTime.Now.ToString()
将生成类似于
30.01.2017 10:30:00
01/30/2017 10:30:00
的字符串

是一个函数,因此您需要删除它,例如手动格式化时间戳:

string filename = "saveFile_" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".txt"
另外,我建议不要使用
+
构建路径,有一个内置函数:

System.IO.Path.Combine(desktopPath, filename);    
// or if you have another folder for those files
System.IO.Path.Combine(desktopPath, "FolderX", filename); 

只需将斜杠替换为破折号:

DateTime.Now.ToString().Replace("/", "-");

DateTime.Now.ToString()
将包含一个
,这是一个。另一方面,如果有任何应用程序将文件转储到我的桌面上,我会很不高兴