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

如果获得应用程序的物理路径(相对路径)呢?对此,我做了一下总结,代码如下:

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 引用!

 

posted @ 2013-08-04 16:41  狼者为王  阅读(259)  评论(0编辑  收藏  举报