Answers:
该ServerUtility班可作为你的一个实例HttpContext。如果您所处的环境知道它将在ASP.Net管道内执行,则可以使用
HttpContext.Current.Server.MapPath()不过,您必须导入System.Web。
您是否可以仅添加一个引用System.Web,然后就可以使用Server.MapPath?
编辑:如今,我建议使用HostingEnvironment.MapPath方法:
这是System.Web程序集中的静态方法,可将虚拟路径映射到服务器上的物理路径。它并不需要一个参考HttpContext。
System.Reflection.Assembly.GetAssembly(type).Location如果您要获取的文件是某个类型的汇编位置。但是,如果文件相对于程序集位置,则可以将其与System.IO名称空间一起使用,以获取文件的确切路径。
class test
{
public static void useServerPath(string path)
{
   if (File.Exists(path)
{
 \\...... do whatever you wabt
}
else
{
\\.....
}
}现在,当您从后面的代码中调用方法时
例如 :
protected void BtAtualizacao_Click(object sender, EventArgs e)
        {
             string path = Server.MapPath("Folder") + "\\anifile.txt";
            test.useServerPath(path);
}这样,您的代码就很简单,并且通过一种方法,您可以为每个调用使用多个路径:)
这对我有帮助
//System.Web.HttpContext.Current.Server.MapPath //        
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/File.txt"),
FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);server.mappath(“”)将在aspx页面上运行,如果要从类文件中获取绝对路径,则必须使用以下命令:
HttpContext.Current.Server.MapPath("~/EmailLogic/RegistrationTemplate.html")