获取应用程序目录的完整路径

时间:2011-07-07 13:45:58

标签: c# asp.net path directory

我有一个使用ASP.NET和C#的网站。

我正在尝试做这样的事情

bitmap.Save(@"C:\Documents and Settings\Berzon\Desktop\Kinor\kWebGUI\Images\" + imageName + ".png")

但是我不想写整个路径,因为它从一台计算机变为另一台计算机 如何使用C#获取完整路径? (此路径是当前正在保存的应用程序)

8 个答案:

答案 0 :(得分:9)

使用此:

bitmap.Save(System.IO.Path.Combine(Server.MapPath("~/RELATIVE PATH OF YOUR APPLICATION"), imageName + ".png"));

或者HttpContext.Current.Request的某些属性(例如ApplicationPathAppDomain.CurrentDomain.BaseDirectory

答案 1 :(得分:2)

检索应用程序路径

string appPath = HttpContext.Current.Request.ApplicationPath;

将虚拟应用程序路径转换为物理路径

string physicalPath = HttpContext.Current.Request.MapPath(appPath);

答案 2 :(得分:2)

System.IO.Path.Combine(Server.MapPath("~"), "Folder1\\Folder2\\etc")

您可以阅读有关MapPath here

的信息

答案 3 :(得分:1)

AppDomain.CurrentDomain.BaseDirectory

System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,image.png)

如果我理解你的问题,你想在asp.net应用程序所在的地方保存它

其他VMATM ans是完美的

答案 4 :(得分:1)

你可以使用

Server.MapPath("imageName + ".png");

答案 5 :(得分:0)

Server.MapPath可以提供帮助。 Read this

答案 6 :(得分:0)

我更喜欢这样解决:

string strPath = string.Format("{0}\\{1}.{2}", HttpContext.Current.Server.MapPath("~\\Images"), imageName, ".png");
bitmap.Save(strPath);

我更喜欢这种方法的原因是: A)很容易通过调试器逐步完成并查看strPath是什么,更容易理解正在发生的事情并修复它不符合您的期望。 B)使用“+”来连接字符串是一个坏习惯。它的可读性较差,每次连接字符串时,内存都会被重新分配......这意味着性能会降低。您应该使用string.Format或StringBuilder。

答案 7 :(得分:0)

为了获得应用程序根目录的路径,如果你在aspx页面中,你可以使用:

Server.MapPath("~/");

但是如果你在另一个不从Page继承的类中,你可以使用:

System.Web.HttpContext.Current.Server.MapPath("~/");

之后使用路径组合来获取特定文件的路径

Path.Combine(root, pathFromRootToFile);
相关问题