[DllImport("user32.dll", EntryPoint = "SetWindowText")]
public static extern int SetWindowText(
IntPtr hwnd,
string lpString
);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private string _Path = @".\\xx\\xx\\进程名称.exe";
private void OnClick(object sender, EventArgs e)
{
try
{
Process[] pro = Process.GetProcesses();
for (int i = 0; i < pro.Length; i++)
{
if (pro[i].ProcessName == "进程名称")
{
pro[i].Kill();
Thread.Sleep(1000);
break;
}
if (pro[i].ProcessName == "进程名称")
{
pro[i].Kill();
Thread.Sleep(1000);
break;
}
}
Process p = new Process();
p.StartInfo.FileName = _Path;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
p.Start();
SpinWait.SpinUntil(delegate { return p.MainWindowHandle != IntPtr.Zero; }); SetWindowText(p.MainWindowHandle, "标题栏名称");
SetForegroundWindow(p.MainWindowHandle);
}
catch (Exception ex)
{
MessageBox.Show(this, ex.Message, "Error");
}
}
//读写ini类//
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key, string def, StringBuilder retVal,
int size, string filePath);
[DllImport("user32.dll")]
static extern void BlockInput(bool Block);
public string IniReadValue(string Section, string Key, string filepath)//对ini文件进行读操作的函数
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp,
255, filepath);
return temp.ToString();
}
C# 启动外部exe并修改窗口标题、INI文件读取
最新推荐文章于 2024-04-23 22:35:38 发布