如何从Visual Studio解决方案/项目中读取文件?

时间:2011-08-18 16:02:30

标签: c# asp.net

 FileInfo template = new  FileInfo(@"C:\Users\bryan\Desktop\ReportTemplate.xlsx");
 template.IsReadOnly = false;

这对测试很好,但我在项目解决方案中有ReportTemplate.xlsx。如何在桌面上使用该文件而不是本地文件?如何在解决方案中引用文件?

2 个答案:

答案 0 :(得分:2)

您想使用Server.MapPath();

确保您想要的文件已添加到项目中:文件 - >添加现有

FileInfo template = new  FileInfo(Server.MapPath("~/Directory/ReportTemplate.xlsx");
template.IsReadOnly = false;

~代表项目的“根”。

我认为这只适用于ASP.NET。

答案 1 :(得分:0)

取决于解决方案中的文件所在的位置。主文件夹是您构建的项目文件夹中的\ bin \ debug。如果要引用项目文件夹根目录中的文件,请使用“.. \ .. \ yourfilename.xlsx”