简体   繁体   English

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

[英]get full path of application directory

I have a website that is using ASP.NET and C#.我有一个使用 ASP.NET 和 C# 的网站。

I am trying to do something like this我正在尝试做这样的事情

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

But I dont want to have to write that whole path, since it changes from computer to computer.但我不想写整个路径,因为它会随着计算机的变化而变化。
How can I get the full path with C#?如何获得 C# 的完整路径? (this path is were the application is currently being saved) (此路径是当前正在保存的应用程序)

Use this:用这个:

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

Or some of properties of HttpContext.Current.Request ( ApplicationPath or AppDomain.CurrentDomain.BaseDirectory , for example)或 HttpContext.Current.Request 的某些属性(例如ApplicationPathAppDomain.CurrentDomain.BaseDirectory

Retrieve the application path检索应用程序路径

string appPath = HttpContext.Current.Request.ApplicationPath;

Convert virtual application path to a physical path将虚拟应用程序路径转换为物理路径

string physicalPath = HttpContext.Current.Request.MapPath(appPath);
System.IO.Path.Combine(Server.MapPath("~"), "Folder1\\Folder2\\etc")

You can read about MapPath here您可以 在此处阅读有关 MapPath 的信息

AppDomain.CurrentDomain.BaseDirectory

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

if i understood your question you want to save it in afolder where your asp.net application is located如果我理解您的问题,您想将其保存在 asp.net 应用程序所在的文件夹中

else VMATM ans is perfect否则 VMATM ans 是完美的

you can use您可以使用

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

Server.MapPath can help could help. Server.MapPath 可以提供帮助。 Read this读这个

I prefer to solve it like this:我更喜欢这样解决它:

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

The reasons I prefer this approach are: A) Its very easy to step through with the debugger and see what strPath is, easier to understand what's happening and fix if its not what you expect.我更喜欢这种方法的原因是:A)它很容易通过调试器单步执行并查看 strPath 是什么,更容易理解正在发生的事情并在它不是您期望的情况下进行修复。 B) Using "+" to concatenate strings is a bad habit. B)使用“+”连接字符串是一个坏习惯。 It's less readable, and every time you concatenate the strings the memory is re-allocated... that means lower performance.它的可读性较差,每次连接字符串时,都会重新分配 memory ......这意味着性能较低。 You should instead use string.Format OR StringBuilder.您应该改用 string.Format 或 StringBuilder。

In order to get the path to the application root, if you are in a aspx page you can use:为了获取应用程序根目录的路径,如果您在 aspx 页面中,您可以使用:

Server.MapPath("~/");

But if you are in another class that doesn't inherit from Page you can use:但是,如果您在另一个不从 Page 继承的 class 中,您可以使用:

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

After that use path combine to get the path to the specific file之后使用路径组合来获取特定文件的路径

Path.Combine(root, pathFromRootToFile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM