[英]How to read file from Visual Studio solution/project?
FileInfo template = new FileInfo(@"C:\Users\bryan\Desktop\ReportTemplate.xlsx");
template.IsReadOnly = false;
This is fine for test, but I have the ReportTemplate.xlsx inside of my project solution.这很适合测试,但我的项目解决方案中有 ReportTemplate.xlsx。 How can I use that file instead of a local one on my desktop?
我如何使用该文件而不是桌面上的本地文件? How do I reference the file inside the solution?
如何引用解决方案中的文件?
You want to use Server.MapPath();
你想使用
Server.MapPath();
Make sure the file you want has been added to the project: File->Add Existing确保你想要的文件已经添加到项目中:File->Add Existing
FileInfo template = new FileInfo(Server.MapPath("~/Directory/ReportTemplate.xlsx");
template.IsReadOnly = false;
The ~
represents "root" of the project. ~
代表项目的“根”。
I think this will only work with ASP.NET.我认为这只适用于 ASP.NET。
It depends where the file in the solution is located.这取决于解决方案中文件的位置。 The home folder is \bin\debug in the project folder which you build.
主文件夹是您构建的项目文件夹中的 \bin\debug。 If you want to reference the file which is in the root of you project folder use "..\..\yourfilename.xlsx"
如果要引用项目文件夹根目录中的文件,请使用“..\..\yourfilename.xlsx”
File Properties->Copy to Output Directory文件属性->复制到Output目录
Then you can access it with "ReportTemplate.xlsx"然后您可以使用“ReportTemplate.xlsx”访问它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.