c#取得控制台应用程序根目录
2016-03-23 08:48
555 查看
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;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称
新建一个解决方案,添加一个类库项目,然后再添加一个控制台项目和 Web 项目。项目结构如图:
![](https://images2015.cnblogs.com/blog/497349/201706/497349-20170625190855741-1641485785.png)
首先,在类库项目里添加一个类 ProjectPath。代码如下:
在控制台项目里测试,代码如下:
![](https://images2015.cnblogs.com/blog/497349/201706/497349-20170625191145991-2144980528.jpg)
控制台程序怎么得到文件路径
------解决方案--------------------
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
方法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 项目。项目结构如图:
![](https://images2015.cnblogs.com/blog/497349/201706/497349-20170625190855741-1641485785.png)
首先,在类库项目里添加一个类 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(); } }
![](https://images2015.cnblogs.com/blog/497349/201706/497349-20170625191145991-2144980528.jpg)
控制台程序怎么得到文件路径
------解决方案--------------------
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
相关文章推荐
- 转载--C# PLINQ 内存列表查询优化历程
- 【C#进阶系列】14 字符、字符串和文本编码
- 【C#进阶系列】13 字符、字符串和文本编码
- C#中的Invoke
- C# TimeSpan计算时间间隔
- c# Invoke和BeginInvoke 区别
- C# 选择文件 和 选择文件夹 对话框
- C# Stream 和 byte[] 之间的转换
- C#泛型学习(一)。
- (转)C#的 GC工作原理基础
- C#语言 数据类型 变量常量 数据转换 3.22
- C# 理解Thread.Sleep()
- C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- C#中string和byte[]相互转换问题解决
- c#多维数组
- C#中委托和Lambda表达式
- 文件
- C#如何调用其他.config配置文件,就是2个乃至3个以上的config文件
- C#机房重构——导出excel表
- ffmpeg从rtsp抓流存flv[c# NReco.VideoConverter flv]