简体   繁体   English

读取.NET项目中的文件

[英]Reading files in a .NET project

I have a C# .NET web project that I'm currently working on. 我有一个正在处理的C#.NET Web项目。 What I'm trying to do is read some files that I dropped into a dir which is at the same level as fileReader.cs which is attempting to read them. 我要尝试执行的操作是读取一些放入到dir的文件,该dir与试图读取文件的fileReader.cs处于同一级别。 On a normal desktop app the following would work: 在普通的桌面应用程序上,可以进行以下操作:

DirectoryInfo di = new DirectoryInfo(./myDir);

However because it's a web project the execution context is different, and I don't know how to access these files? 但是,由于它是一个Web项目,因此执行上下文不同,并且我不知道如何访问这些文件?

Eventually fileReader will be called in an installation routine. 最终,将在安装例程中调用fileReader。 I intend to override one of the Installer.cs' abstract methods so will this affect the execution context? 我打算重写Installer.cs的抽象方法之一,所以这会影响执行上下文吗?

使用Server.MapPath获取当前正在执行的页面的本地路径。

Use the Server.MapPath method whichs maps the specified relative or virtual path to the corresponding physical directory on the server. 使用Server.MapPath方法将指定的相对或虚拟路径映射到服务器上的相应物理目录。

Server.MapPath("mydir/file.some")

This returns: C:\\site\\scripts\\mydir\\file.some 这将返回: C:\\ site \\ scripts \\ mydir \\ file.some

Script also can call the MapPath with full virtual path: 脚本还可以使用完整的虚拟路径调用MapPath:

Server.MapPath("/scripts/mydir/file.some")

Here is the link to the MSDN documentation of MapPath . 这是MapPath的MSDN文档的链接

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

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