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

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. iOS 归档

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  2. [转]EntityFramework走马观花之CRUD(上)

    学习Entity Framework技术期间查阅的优秀文章,出于以后方便查阅的缘故,转载至Blog,可查阅原文:http://blog.csdn.net/bitfan/article/details/ ...

  3. JQuery实现页面企业广告图片切换和新闻列表滚动效果

    最近用到一个页面上图片左右切换和新闻列表滚动呈现的效果,整理如下: 前段代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit ...

  4. Canvas中鼠标获取元素并拖动技术

    Silverlight拖动,需要Canvas. Canvas管网定义: 定义一个区域,在该区域中可以使用相对于该区域的坐标显式定位子元素. XAML <Canvas ...> oneOrM ...

  5. CareerCup: 17.14 minimize unrecognized characters

    Oh, no! You have just completed a lengthy document when you have an unfortu- nate Find/Replace misha ...

  6. csuoj 1115: 最短的名字

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1115 1115: 最短的名字 Time Limit: 5 Sec  Memory Limit: 6 ...

  7. HDU 4718 The LCIS on the Tree(树链剖分)

    Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i < ...

  8. paper 27 :图像/视觉显著性检测技术发展情况梳理(Saliency Detection、Visual Attention)

    1. 早期C. Koch与S. Ullman的研究工作. 他们提出了非常有影响力的生物启发模型. C. Koch and S. Ullman . Shifts in selective visual ...

  9. WMI技术介绍和应用——查询硬件信息

    //查询得到系统盘所在硬盘的ID SELECT DiskIndex FROM Win32_DiskPartition WHERE Bootable = TRUE //如何使用WMI查询系统盘所在硬盘的 ...

  10. thinphp讲解(三)——空操作、空控制器、跨控制器、命名空间

    一.“空操作”本质意思:一个对象(控制器)调用本身不存在的操作方法 一般网站处于安全考虑不给用户提示任何错误信息 在tp里面控制器controller.class.php里有个_call()方法 所以 ...