C#:Server.Mappath如何读取文件? [英] C#: How can Server.Mappath read a file?

查看:307
本文介绍了C#:Server.Mappath如何读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Visual Studio 2008 solution that contains a handful of projects. One project contains a WCF Service I'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:

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


 Project2
  filefolder
    myfile.txt

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

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

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

似乎都无法访问该文件.我想到的一件事:Visual Studio 2008在 IIS 中的自己的沙箱中运行项目和WCF. .这可能是问题吗?如果在常规IIS中进行设置和部署,是否可以正常工作?

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?

推荐答案

问题是,调用WCF时,文件系统一直运行到bin/Debug文件夹.因此,尝试从那里进行MapMath无效.回溯工作的路径:

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:

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

那行得通.感谢所有帮助人员!

That worked. Thanks for all the help guys!

这篇关于C#:Server.Mappath如何读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆