首页 技术 正文
技术 2022年11月15日
0 收藏 907 点赞 2,450 浏览 2774 个字

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

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(); .类似1ProcessStartInfo 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.exeSystem.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();
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289