各位老师好,再C#中使用这个去启动外部程序可是在程序所在地址烂填写的地址却无法正常点开,如果填写的地址为【str】则可以正常打开,str为本程序所在的目录,pbt为这个程序所在的目录,他们俩之间有什么区别吗,请问各位老师是我地址填写的不对吗,还是需要增加什么修改方法?因为并不想让2个程序放在一起,请问如何可以在pbt路径下正常打开exe呢?请各位老师指教,谢谢
string str = System.Environment.CurrentDirectory;
[DllImport("shell32.dll")]
public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
string pbt = @"C:\Users\" + username + @"\Desktop\ICO\a";
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("Start.exe"), new StringBuilder(""), new StringBuilder(pbt), 1);
ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(@"C:\Users\" + username + @"\Desktop\ICO\a\Start.exe"), new StringBuilder(""), new StringBuilder(@"C:\Users\" + username + @"\Desktop\ICO\a\Start.exe"), 1);
目标文件和运行目录不能同时使用相对路径,使用StringBuilder先设置CharSet.Unicode。
最后贴上官方文档:https://docs.microsoft.com/zh-cn/windows/win32/api/shellapi/nf-shellapi-shellexecutew
或者你可以用其他方法来
using System;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = true,
Verb = "open",
WorkingDirectory = Environment.CurrentDirectory
};
Process.Start(startInfo);
}
}
}