c#取得控制台应用程序根目录
1、取得控制台应用程序的根目录方法
方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径
方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集
2、取得Web应用程序的根目录方法
方法1、HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。用于App_Data中获取
方法2、Server.MapPath("") 或者 Server.MapPath("~/");//返回与Web服务器上的指定的虚拟路径相对的物理文件路径
方法3、Request.ApplicationPath;//获取服务器上ASP.NET应用程序的虚拟应用程序根目录
3、取得WinForm应用程序的根目录方法
1、Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径
2、Application.StartupPath.ToString();//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称
3、Directory.GetCurrentDirectory();//获取应用程序的当前工作目录
4、AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集
5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称
其中:以下两个方法可以获取执行文件名称
1、Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。
2、Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称
System.IO.Directory.GetCurrentDirectory() //使用静态类 Directory 下的 GetCurrentDirectory 方法获取当前程序的路径 System.Reflection.Assembly.GetCallingAssembly().Location //获取调用该方法的方法所在的程序集,并获取该程序集文件路径(由该文件路径可以得到程序集所在的目录) System.Reflection.Assembly.GetEntryAssembly().Location //获取包含该应用程序入口点的程序集(可执行文件),并获取该程序集文件的路径(由该文件路径可以得到程序集所在的目录) System.Reflection.Assembly.GetExecutingAssembly().Location //获取执行该方法的程序集,并获取该程序集的文件路径(由该文件路径可以得到程序集所在的目录) System.Windows.Forms.Application.StartupPath //获取启动应用程序的可执行文件所在的目录 System.Windows.Forms.Application.ExecutablePath //获取启动应用程序的可执行文件的路径(由该文件路径可以得到应用程序所在的目录)
新建一个解决方案,添加一个类库项目,然后再添加一个控制台项目和 Web 项目。项目结构如图:
首先,在类库项目里添加一个类 ProjectPath。代码如下:
namespace Common { public class ProjectPath { public static Dictionary<string, string> GetCurrentPaths() { Dictionary<string, string> paths = new Dictionary<string, string>(); paths.Add("System.AppDomain.CurrentDomain.BaseDirectory", System.AppDomain.CurrentDomain.BaseDirectory); paths.Add("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase", System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase); paths.Add("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName", System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); paths.Add("System.Environment.CurrentDirectory", System.Environment.CurrentDirectory); paths.Add("System.IO.Directory.GetCurrentDirectory()", System.IO.Directory.GetCurrentDirectory()); paths.Add("System.Reflection.Assembly.GetCallingAssembly().Location", System.Reflection.Assembly.GetCallingAssembly().Location); try {/* System.Reflection.Assembly.GetEntryAssembly() 方法被 Web 应用程序调用时返回值是 null, 访问其 Location 属性将产生异常 */ paths.Add("System.Reflection.Assembly.GetEntryAssembly().Location", System.Reflection.Assembly.GetEntryAssembly().Location); } catch { } paths.Add("System.Reflection.Assembly.GetExecutingAssembly().Location", System.Reflection.Assembly.GetExecutingAssembly().Location); paths.Add("System.Windows.Forms.Application.StartupPath", System.Windows.Forms.Application.StartupPath); paths.Add("System.Windows.Forms.Application.ExecutablePath", System.Windows.Forms.Application.ExecutablePath); //Web 项目特有方式,必需在 HTTP 上下文中才能访问 Server 对象 //paths.Add("Server.MapPath(\"~\")", Server.MapPath("~")); return paths; } } }
在控制台项目里测试,代码如下:
class Program { static void Main(string[] args) { Dictionary<string, string> paths = ProjectPath.GetCurrentPaths(); foreach (string key in paths.Keys) { Console.WriteLine(key); Console.WriteLine(paths[key]); Console.WriteLine(); } Console.Read(); } }
控制台程序怎么得到文件路径
------解决方案--------------------
string filePath=Application.StartupPath.Replace("bin\Debug","myfile.txt")这样就可以得到你想要的结果了```
//Console.ForegroundColor = ConsoleColor.Green; 设置cmd窗口字体颜色
参考:http://www.cnblogs.com/zhhh/archive/2012/06/30/2571222.html
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。