您当前的位置:首页 > ASP.NET > 文章详情

c# winform获取系统路径的方法

2012-09-304850人围观
1.获取当前进程的完整路径,包含文件名(进程名)。 this.GetType().Assembly.Location; 返回格式: X:\xxx\xxx\xxx.exe 2.获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; 返回格式: X:\xxx\xxx\xxx.exe 3.获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。 System.Environment.CurrentDirectory; 返回格式: X:\xxx\xxx 4.获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。 System.AppDomain.CurrentDomain.BaseDirectory; 返回格式: X:\xxx\xxx\ 5.获取和设置包含该应用程序的目录的名称。 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 返回格式: X:\xxx\xxx\ (.exe文件所在的目录+"\") 6.获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。 System.Windows.Forms.Application.StartupPath; 返回格式: X:\xxx\xxx string path=System.Windows.Forms.Application.StartupPath //path为你的C/S程序启动路径,你可以在此路径基础上调用或设置相对路径 在.net中用 ".. "来表示相对路径会有一个问题,如果当前程序中有用OpenfileDialog,来打开文件.而且你所打开的文件不在当前程序所在的目录,那么你再次调用相对路径的时候,你会发现路径已经指到你曾经打开过文件的路径了. 所以我建议这样 stringThePath=System.IO.Path.GetDirectoryName(Application.ExecutablePath); Imageimage=Image.FromFile(ThePath+ "\\..\\..\\Image\\1.gif "); 7.获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。 System.Windows.Forms.Application.ExecutablePath; 返回格式: X:\xxx\xxx\xxx.exe 8.获取应用程序的当前工作目录。(注:此方法取值不固定,随着OpenFileDialog、SaveFileDialog等对象所确定的目录而改变,解决办法,程序启动时System.IO.Directory.GetCurrentDirectory()保存到变量里,需要程序所在路径是,使用变量值。) System.IO.Directory.GetCurrentDirectory(); 返回格式: X:\xxx\xxx 9.WebForm使用:获取站点所在虚拟目录的物理路径 Request.PhysicalApplicationPath 返回格式: X:\xxx\xxx 10.  在卸载程序获取系统安装的目录: System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly(); string path=curPath.Location;//得到安装程序类SetupLibrary文件的路径,获取这个文件路径所在的目录即得到安装程序的目录

文章评论

热门评论
暂无评论