C#:获取执行程序所在路径和启动资源管理器

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C#:获取执⾏程序所在路径和启动资源管理器
⼀、获取执⾏程序所在路径
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);
1.直接启动
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = bine(Environment.GetEnvironmentVariable("windir"), "explorer.exe");
Process.Start(info).WaitForExit();
2.类似1
ProcessStartInfo info = new ProcessStartInfo();
info.CreateNoWindow = true;
eShellExecute = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.FileName = bine(Environment.GetEnvironmentVariable("windir"), "explorer.exe");
Process.Start(info);
3.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 = 0,
SW_SHOWNORMAL = 1,
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_FORCEMINIMIZE = 11,
SW_MAX = 11
}
[DllImport("shell32.dll")]
static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
ShowCommands nShowCmd);
4.shell窗⼝常规
Process.Start(bine(Environment.GetEnvironmentVariable("windir"), "explorer.exe"));
ShellWindows win= new SHDocVw.ShellWindows();
5.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;
eShellExecute = false;
process.Start();
process.StandardInput.WriteLine(Environment.GetEnvironmentVariable("windir")+"\\explorer.exe"); process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
View Code。

相关文档
最新文档