简体   繁体   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;

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.

相关问题 MS Visual Studio - 如何从文件系统导入项目或解决方案并知道它是什么类型的项目 - MS Visual Studio - how to import a project or solution from file system and know what kind of project it is 如何在Visual Studio解决方案的项目中检查文件是否存在于文件夹中 - How to check if file exists in a folder in my project in Visual Studio solution 如何从NANT构建文件创建Visual Studio解决方案和项目文件 - How to create Visual Studio solution and project files from NANT build file 如何从Visual Studio中的项目解决方案中删除其他构建文件 - How to Delete Additional build files from the Project Solution in visual studio 如何从 Visual Studio Package 项目中获取当前的解决方案名称? - How to get the current solution name from a Visual Studio Package project? 从 Visual Studio 解决方案中提取项目作为单独的项目 - Extracting a project as a separate project from the Visual Studio solution 在 Visual Studio 中重命名项目/解决方案 - Renaming project / solution in Visual studio 通过Visual Studio Designer确定项目的解决方案文件位置 - Determine a project's solution file location via Visual Studio Designer 从Visual Studio中的解决方案文件夹创建项目模板 - Create Project template from solution folder in visual studio 将项目从OutSystems转换为C#Visual Studio解决方案 - Convert a project from OutSystems to C# Visual Studio solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM