我有一个使用ASP.NET和C#的网站。
我想做类似于这样的事情:
bitmap.Save(@"C:\Documents and Settings\Berzon\Desktop\Kinor\kWebGUI\Images\" + imageName + ".png")
但我不想每次都手动输入完整的路径,因为它会因计算机而异。
有什么办法可以用C#获取完整的路径?(这是应用程序当前保存的位置)
bitmap.Save(System.IO.Path.Combine(Server.MapPath("~/RELATIVE PATH OF YOUR APPLICATION"), imageName + ".png"));
或者一些HttpContext.Current.Request的属性(例如ApplicationPath或AppDomain.CurrentDomain.BaseDirectory)
获取应用程序路径
string appPath = HttpContext.Current.Request.ApplicationPath;
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
System.IO.Path.Combine(Server.MapPath("~"), "Folder1\\Folder2\\etc")
您可以在这里了解有关MapPath的信息。
AppDomain.CurrentDomain.BaseDirectory
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,image.png)
如果我理解你的问题,你想将它保存在你的 ASP.NET 应用程序所在的文件夹中
否则 VMATM 的答案是完美的
你可以使用
Server.MapPath("imageName + ".png");
string strPath = string.Format("{0}\\{1}.{2}", HttpContext.Current.Server.MapPath("~\\Images"), imageName, ".png");
bitmap.Save(strPath);
Server.MapPath("~/");
但如果你在另一个不继承自Page的类中,你可以使用:
System.Web.HttpContext.Current.Server.MapPath("~/");
然后使用路径组合来获取特定文件的路径
Path.Combine(root, pathFromRootToFile);