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

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

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();//路径到inDebug
            string path = currentPath.Split(new string[] { @"in" },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 引用!

原文地址:https://www.cnblogs.com/wangjianhui008/p/3236505.html