C#:Server.Mappath 如何读取文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/987082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:58:19  来源:igfitidea点击:

C#: How can Server.Mappath read a file?

c#wcffile-iomapping

提问by Blaze

I have a Visual Studio 2008 solution that contains a handful of projects. One project contains a WCF ServiceI'm deploying. That WCF Service references some code in one of the other projects. That code is trying to read a file that's in a folder in the WCF project. Pseudo-project structure:

我有一个包含少数项目的 Visual Studio 2008 解决方案。一个项目包含我正在部署的WCF 服务。该 WCF 服务引用了其他项目之一中的一些代码。该代码正在尝试读取 WCF 项目文件夹中的文件。伪项目结构:

Solution
 Project1
  myclass.cs
    string file = Server.Mappath("");


 Project2
  filefolder
    myfile.txt

What is the correct syntax to put in the Mappath? I've tried all different variations such as:

放入 Mappath 的正确语法是什么?我尝试了所有不同的变体,例如:

".filefolder/myfile.txt"
"/filefolder/myfile.txt"
"./filefolder/myfile.txt"
"~/filefolder/myfile.txt"

None seem to be able to reach the file. One thing I thought of: Visual Studio 2008 runs the project and WCF in its own sandbox in IIS. Could that be the issue? Would it work if setup and deployed in regular IIS?

似乎没有人能够访问该文件。我想到的一件事:Visual Studio 2008 在自己的IIS沙箱中运行项目和 WCF 。这可能是问题吗?如果在常规 IIS 中设置和部署它会起作用吗?

采纳答案by Blaze

The problem is that when invoking the WCF, the file system runs all the way out to the bin/Debug folder. So trying to MapMath from there doesnt work. Backtracking up the path worked:

问题是在调用 WCF 时,文件系统一直运行到 bin/Debug 文件夹。所以从那里尝试 MapMath 是行不通的。回溯路径有效:

filedata = File.ReadAllBytes("../../filefolder/myfile.txt");

That worked. Thanks for all the help guys!

那奏效了。感谢所有帮助的家伙!

回答by John Saunders

The best thing would be to not do this to begin with.

最好的办法是一开始就不要这样做。

Why would you want to tie the deployment location of one project to the deployment location of another? If you need the WCF service to read the file, then copy the file to the WCF service.

为什么要将一个项目的部署位置绑定到另一个项目的部署位置?如果需要 WCF 服务读取文件,则将文件复制到 WCF 服务。

回答by jr3

From MSDN; Because the path parameters in the following examples do not start with a slash character, they are mapped relative to the directory that contains the example file.

来自 MSDN;由于以下示例中的路径参数不以斜杠字符开头,因此它们相对于包含示例文件的目录进行映射。

Try:

尝试:

Server.Mappath("filefolder/somefile.file");

回答by Steve Dignan

Have you tried using HostingEnvironment.ApplicationPhysicalPath?

您是否尝试过使用HostingEnvironment.ApplicationPhysicalPath

var fileInfo = new FileInfo(
    Path.Combine( new DirectoryInfo( HostingEnvironment.ApplicationPhysicalPath ).Parent.Name , @"filefolder/myfile.txt" ) );

回答by corymathews

Server.MapPath(Path.Combine( new DirectoryInfo( HostingEnvironment.ApplicationPhysicalPath ).Parent.Name , "Filename.txt" ));

Seems to work for me. I did need to include

似乎对我有用。我确实需要包括

using System.Web.Hosting;

回答by wildcat

var serverPath =
       System.Web.Hosting.HostingEnvironment.MapPath("~/filefolder/myfile.txt");