在 ASP.NET 的外部 C# 类中使用 Server.MapPath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1190196/
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 10:25:20  来源:igfitidea点击:

Using Server.MapPath in external C# Classes in ASP.NET

c#asp.netfilepathrelative-path

提问by Chet

I'm trying to get the absolute path of certain files in a C# class. Server.MapPathworks great of course for ASPX and their code-behind pages, but that doesn't exist in another class file. I tried HostingEnvironment.MapPath(), but that complains that the relative virtual path isn't allowed. Any thoughts?

我正在尝试获取 C# 类中某些文件的绝对路径。 Server.MapPath当然,对于 ASPX 及其代码隐藏页面来说效果很好,但在另一个类文件中不存在。我试过HostingEnvironment.MapPath(),但抱怨相对虚拟路径是不允许的。有什么想法吗?

System.Webis already imported.

System.Web已经导入。

采纳答案by womp

The ServerUtilityclass is available as an instance in your HttpContext. If you're in an environment where you know it'll be executed inside the ASP.Net pipeline, you can use

ServerUtility班可作为你的一个实例HttpContext。如果您处于知道它将在 ASP.Net 管道内执行的环境中,则可以使用

HttpContext.Current.Server.MapPath()

You'll have to import System.Webthough.

不过,您必须导入System.Web

回答by Dan Diplo

Can't you just add a reference to System.Weband then you can use Server.MapPath?

你不能只添加一个引用System.Web然后你可以使用Server.MapPath吗?

Edit: Nowadays I'd recommend using the HostingEnvironment.MapPathMethod:

编辑:现在我建议使用HostingEnvironment.MapPath方法

It's a static method in System.Webassembly that Maps a virtual path to a physical path on the server. It doesn'trequire a reference to HttpContext.

它是System.Web程序集中的一种静态方法,它将虚拟路径映射到服务器上的物理路径。它不需要HttpContext.

回答by David McEwing

System.Reflection.Assembly.GetAssembly(type).Location

IF the file you are trying to get is the assembly location for a type. But if the files are relative to the assembly location then you can use this with System.IOnamespace to get the exact path of the file.

如果您尝试获取的文件是类型的程序集位置。但是如果文件是相对于程序集位置的,那么你可以使用它和System.IO命名空间来获取文件的确切路径。

回答by Ahmad

class test
{
public static void useServerPath(string path)
{
   if (File.Exists(path)
{
 \...... do whatever you wabt
}
else
{
\.....
}
}

Now when you call the method from the codebehind

现在,当您从代码隐藏中调用该方法时

for example :

例如 :

protected void BtAtualizacao_Click(object sender, EventArgs e)
        {
             string path = Server.MapPath("Folder") + "\anifile.txt";

            test.useServerPath(path);
}

in this way your code is to simple and with one method u can use multiple path for each call :)

通过这种方式,您的代码很简单,使用一种方法,您可以为每次调用使用多个路径:)

回答by soamazing

I use this too:

我也用这个:

System.Web.HTTPContext.Current.Server.MapPath

回答by Yakir Manor

you can also use:

您还可以使用:

var path = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data/myfile.txt")

if

如果

var path = Server.MapPath("~/App_Data");
var fullpath = Path.Combine(path , "myfile.txt");

is inaccessible

无法访问

回答by netuser

This one helped for me

这个对我有帮助

//System.Web.HttpContext.Current.Server.MapPath //        
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/File.txt"),
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

回答by daudihus

Whether you're running within the context of ASP.NET or not, you should be able to use HostingEnvironment.ApplicationPhysicalPath

无论您是否在 ASP.NET 的上下文中运行,您都应该能够使用 HostingEnvironment.ApplicationPhysicalPath

回答by Debendra Dash

The server.mappath("") will work on aspx page,if you want to get the absolute path from a class file you have to use this-

server.mappath("") 将在 aspx 页面上工作,如果你想从类文件中获取绝对路径,你必须使用这个 -

HttpContext.Current.Server.MapPath("~/EmailLogic/RegistrationTemplate.html")