繁体   English   中英

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

[英]How to read file from Visual Studio solution/project?

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

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

你想使用Server.MapPath();

确保你想要的文件已经添加到项目中:File->Add Existing

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

~代表项目的“根”。

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

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

文件属性->复制到Output目录

然后您可以使用“ReportTemplate.xlsx”访问它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM