C# 获取错误“;不支持URI格式;

C# 获取错误“;不支持URI格式;,c#,file,C#,File,在这里,我试图生成excel文件并将此文件保存到我的本地应用程序文件夹,但出现错误“URI格式不受支持”。有人能告诉我为什么要这样做吗 File.WriteAllText(ClsCommon.ApplicationPath + "TR_Temp/" + "AssessmentSheet.xls", renderedGridView); 在评论中,您提到ClsCommon.ApplicationPath是“localhost:1682/TR WEB Corp”。也就是说,您正试图写入路径为“lo

在这里,我试图生成excel文件并将此文件保存到我的本地应用程序文件夹,但出现错误“URI格式不受支持”。有人能告诉我为什么要这样做吗

File.WriteAllText(ClsCommon.ApplicationPath + "TR_Temp/" + "AssessmentSheet.xls", renderedGridView);

在评论中,您提到
ClsCommon.ApplicationPath
“localhost:1682/TR WEB Corp”
。也就是说,您正试图写入路径为“localhost:1682/TR-WEB-CorpTR\u Temp/AssessmentSheet.xls”的文件

这不是路径,而是URI。路径是本地或网络磁盘上的内容,应该以驱动器名(
C:
D:
等)或网络共享名(
\\network\share
)开头

此外,您的路径似乎在
ClsCommon.ApplicationPath
“TR_Temp/”
之间缺少路径分隔符(反斜杠)。正如@Heslacher在评论中提到的,最好使用
System.IO.Path.Combine()
来避免此类错误。

使用这些方法

System.IO.Path.GetTempPath()
System.IO.Path.Combine()
您可以获得有效的文件名,如:

string fileName = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "TR_Temp","AssessmentSheet.xls");
File.WriteAllText(fileName, renderedGridView);
将renderedGridView的内容写入文件。
请参阅:

什么是
ClsCommon.ApplicationPath
?始终使用System.IO.Path.Combine()创建路径和文件名。ClsCommon.ApplicationPath提供我的本地应用程序路径。它是一个comman类,我在其中为应用程序编写属性path@Sandeep我想乔恩问的是“您能在应用程序崩溃时发布ClsCommon.ApplicationPath的值吗?”@SANDEEP,Tarik、RB和Jon的意思是在这里写下您的ClsCommon.ApplicationPath的值,如:C:\Users\。。。