一、 获取执行程序所在路径

1.获取和设置当前目录的完全限定路径。

string str = System.Environment.CurrentDirectory;  //获取的是主程序目录,线程启动的子程序内获取的路径也是主程序的工作目录

Result: C:\xxx\xxx

2.获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str = System.Windows.Forms.Application.StartupPath;  //exe 执行目录

Result: C:\xxx\xxx

3.获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名。

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

Result: C:\xxx\xxx\xxx.exe

4.获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。

string str = System.AppDomain.CurrentDomain.BaseDirectory;

Result: C:\xxx\xxx\

5.获取应用程序的当前工作目录。

string str = System.IO.Directory.GetCurrentDirectory();

Result: C:\xxx\xxx

6.获取和设置包含该应用程序的目录的名称。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

Result: C:\xxx\xxx\

7.获取当前进程的完整路径,包含文件名。

string str = this.GetType().Assembly.Location;

Result: C:\xxx\xxx\xxx.exe

8.获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。string str = System.Windows.Forms.Application.ExecutablePath;

Result: C:\xxx\xxx\xxx.exe

此外,更多见的通过XML文件配置具体的路径来达到合理的规划配置文件的具体存放位置,如WEB中的配置文件中的路径。

二、启动资源管理器:
 
 System.Diagnostics.Process.Start("explorer.exe", GlobalInfos.ClassroomRecordPath);
.直接启动

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe");
Process.Start(info).WaitForExit(); .类似1 ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
info.UseShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.FileName = Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe");
Process.Start(info); .shell 外部方法 private void button1_Click(object sender, EventArgs e)
{
ShellExecute(IntPtr.Zero, null, "explorer.exe", null, null, ShowCommands.SW_SHOW);
} public enum ShowCommands : int {
SW_HIDE = ,
SW_SHOWNORMAL = ,
SW_NORMAL = ,
SW_SHOWMINIMIZED = ,
SW_SHOWMAXIMIZED = ,
SW_MAXIMIZE = ,
SW_SHOWNOACTIVATE = ,
SW_SHOW = ,
SW_MINIMIZE = ,
SW_SHOWMINNOACTIVE = ,
SW_SHOWNA = ,
SW_RESTORE = ,
SW_SHOWDEFAULT = ,
SW_FORCEMINIMIZE = ,
SW_MAX = } [DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd); .shell窗口常规 Process.Start(Path.Combine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));
ShellWindows win= new SHDocVw.ShellWindows(); .cmd命令执行explorer.exe System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
process.StartInfo = startInfo;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(Environment.GetEnvironmentVariable("windir")+"\\explorer.exe");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();

C#: 获取执行程序所在路径和启动资源管理器的更多相关文章

  1. delphi根据进程PID获取程序所在路径的函数(用OpenProcess取得句柄,用GetModuleFileNameEx取得程序名)

    uses psapi; {根据进程PID获取程序所在路径的函数}function GetProcessExePath(PID: Cardinal): string;varpHandle: THandl ...

  2. Qt 程序获取程序所在路径、用户目录路径、临时文件夹等特殊路径的方法

    Qt 程序获取程序所在路径.用户目录路径.临时文件夹等特殊路径的方法 经常我们的程序中需要访问一些特殊的路径,比如程序所在的路径.用户目录路径.临时文件夹等.在 Qt 中实现这几个功能所用的方法虽然都 ...

  3. Delphi获取文件名、文件名不带扩展名、文件名的方法;delphi 获取文件所在路径

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  4. bat(续五)-获取批处理文件所在路径

    获取批处理文件所在路径        在开发时,经常需要使用批处理运行一些程序,java程序 犹其是这样,往往需要运行时根路径.Hardcode一个路径总是令自己觉得不自在,例如一个java程序从一台 ...

  5. dotnet 获取程序所在路径的方法

    在 dotnet 有很多方法可以获取当前程序所在的路径,但是这些方法获取到的路径有一点不相同,特别是在工作路径不是当前的程序所在的路径的时候 通过下面几个方法都可以拿到程序所在的文件夹或程序文件 Ap ...

  6. Current_Path 获取脚本所在路径(当前路径),取当前时间做文件名(uformat)

    获取脚本当前所在路径: $CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.Las ...

  7. 一点一点学写Makefile(5)-获取文件所在路径

    我们在开发一套代码时,应该保证工程放到任何一个目录中均可以编译成功,但是有时候链接库的时候会造成编译错误,本次就会告诉大家如何动态的获得工程所在的绝对路径 代码下载目录 选择Makefile-5 // ...

  8. 批处理文件中获取当前所在路径的几种方法,以及写文件到txt

    @echo off setlocal EnableDelayedExpansion echo 当前正在运行的批处理文件所在路径:!cd! pause @echo off echo 当前目录是:%cd% ...

  9. C# WinForm获取程序所在路径方法

    多个获取WinForm程序所在文件夹路径的方法,收藏备忘. 1)获取当前进程的完整路径,包含文件名(进程名). 代码:string str =this.GetType().Assembly.Locat ...

随机推荐

  1. SQLdiag-配置文件-扩展

    CustomDiagnostics在我们第一次双击D:\Program Files\Microsoft SQL Server\100\Tools\Binn目录下的SQLdiag.exe应用程序所收集的 ...

  2. 第三篇 Integration Services:增量加载-Adding Rows

    本篇文章是Integration Services系列的第三篇,详细内容请参考原文. 增量加载是什么增量加载仅加载与先前加载差异的.差异包括:->新增的行->更新的行->删除的行通过 ...

  3. Android基本控件属性设置

    EditText: EditText去边框:<EditText  android:background="@null"  /> EditText的hint的字体大小的设 ...

  4. iOS添加自动更新的代码

    - (void)versionUpdate{ //获得当前发布的版本 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...

  5. Dive into python 实例学python (1) —— 函数和测试

    odbchelper.py def buildConnectionString(params): """Build a connection string from a ...

  6. C++Primer 第九章

    //1.vector:可变大小数组.支持快速随机访问,在尾部之外的位置插入或删除元素可能很慢.注意点:不要在vector中存放bool类型,vector<bool>并不是一个容器,其实现方 ...

  7. poj: 3094

    简单题 #include <iostream> #include <stdio.h> #include <string> #include <stack> ...

  8. bzoj4238 电压

    首先先直接对图进行二染色,dfs染完色后,有的边为搜索树边,有的为非树边,当非树边连接的两头的点为异色的时候,那么很明显这条非树边和树边构成的环上的边必然不可能成为答案:如果非树边的两端的点同色,那么 ...

  9. mysql explain

    我们使用EXPLAIN解析SQL执行计划时,如果有下面几种情况,就需要特别关注下了: 首先看下 type 这列的结果,如果有类型是 ALL 时,表示预计会进行全表扫描(full table scan) ...

  10. fackbook的Fresco的多种图片加载方法以及解码过程

    上篇文章中我们提到了图片加载其实是用了三条线程,如果没看过的同学可以先了解下这里. fackbook的Fresco的Image Pipeline以及自身的缓存机制 那么今天我们就来探索一下如何在代码中 ...