您的位置:首页 > 其它

如何获得应用程序的物理路径

2013-08-04 16:41 369 查看
如果获得应用程序的物理路径(相对路径)呢?对此,我做了一下总结,代码如下:

1. 获得相对路径(winform适用)

/// <summary>
/// 获得应用程序的物理路径(相对路径)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string currentPath = System.IO.Directory.GetCurrentDirectory();//路径到\bin\Debug
string path = currentPath.Split(new string[] { @"\bin" },StringSplitOptions.None)[0];
MessageBox.Show(path);
}


2. 获得相对路径(web方面)

/// <summary>
/// 获得应用程序的物理路径(相对路径)
/// </summary>
/// <returns></returns>
public string GetCurrentPath()
{
string currentPath =System.Web.HttpContext.Current.Server.MapPath("~/bin");
return currentPath;
}


注意一下:方法2 中 必须添加 System.Web 引用!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: