您当前的位置:首页 > 文章摘抄 > 正文

useshellexecute(我现在是用ShellExecute开启了一段程序,不知道怎么关闭,才最好)

本文目录

  • 我现在是用ShellExecute开启了一段程序,不知道怎么关闭,才最好
  • 你好 关于你所说的ShellExecute 我不是不懂 能麻烦一下 具体说说吗
  • 怎样用C#写入DOS命令提示符,并让其执行
  • 如何正确的终止正在运行的子线程
  • C#怎在linux下怎么发起需要root权限的命令
  • process_cmd.StartInfo.UseShellExecute = false;是什么意思啊
  • 在C#中,那条语句是可以控制关机的
  • 怎样在CMD中获取所调用的.exe的返回值

我现在是用ShellExecute开启了一段程序,不知道怎么关闭,才最好

ShellExecute开启的程序,不是很好控制,建议用CreateProcess开启程序,利用返回的piProcinfo参数使用TerminateProcess来关闭程序。bFuncRetn = CreateProcess(NULL, szCmdline, // command line NULL, // process security attributes NULL, // primary thread security attributes TRUE, // handles are inherited 0, // creation flags NULL, // use parent’s environment NULL, // use parent’s current directory &siStartInfo, // STARTUPINFO pointer &piProcInfo); // receives PROCESS_INFORMATION

你好 关于你所说的ShellExecute 我不是不懂 能麻烦一下 具体说说吗

ShellExecute(this-》GetSafeHwnd(), “open“, “calc.exe“, NULL, NULL, SW_SHOW);只是还不能限制它的大小?不能嵌入到指定窗口中去。 再将实验结果赋值过来:HANDLE handle;//process handleHWND apphwnd;//window handle/*************Global functions for hosting******************///Function to enumerate all windows.int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param){ DWORD pID; DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);//get process id if (TpID == (DWORD)param) { apphwnd=hwnd;//hwnd is the window handle return false; } return true;}//Functio to start a orocess and return the process handleHANDLE StartProcess(LPCTSTR program, LPCTSTR args){ HANDLE hProcess = NULL; PROCESS_INFORMATION processInfo; STARTUPINFO startupInfo; ::ZeroMemory(&startupInfo, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); startupInfo.dwFlags = STARTF_USESHOWWINDOW; startupInfo.wShowWindow = SW_HIDE; if(::CreateProcess(program, (LPTSTR)args, NULL, // process security NULL, // thread security FALSE, // no inheritance 0, // no startup flags NULL, // no special environment NULL, // default startup directory &startupInfo, &processInfo)) { /* success */ Sleep(1000); ::EnumWindows(&EnumWindowsProc, processInfo.dwThreadId);//Iterate all windows hProcess = processInfo.hProcess; } /* success */ return hProcess;}void CXXXXXDlg::OnButton1() { CRect rect; GetClientRect(▭); handle=StartProcess(“C:\\WINDOWS\\system32\\mspaint.exe“,““); if(apphwnd!=NULL) { ::SetParent(apphwnd,m_hWnd); SetWindowLong(apphwnd, GWL_STYLE, WS_VISIBLE); ::MoveWindow(apphwnd, rect.left, rect.top,rect.right, rect.bottom, true); } else MessageBox(“Cannot find Window“);}

怎样用C#写入DOS命令提示符,并让其执行

写一个方法cmd就是Dos命令using System.Diagnostics; public void abc(string cmd) { //实例一个Process类,启动一个独立进程 Process p = new Process(); //Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性: p.StartInfo.FileName = “cmd.exe“; //设定程序名 p.StartInfo.Arguments = “/c“+cmd; //设定程式执行参数 p.StartInfo.UseShellExecute = false; //关闭Shell的使用 p.StartInfo.RedirectStandardInput = true; //重定向标准输入 p.StartInfo.RedirectStandardOutput = true; //重定向标准输出 p.StartInfo.RedirectStandardError = true; //重定向错误输出 p.StartInfo.CreateNoWindow = true; //设置不显示窗口 p.Start(); //启动 p.StandardInput.WriteLine(“exit“); //不过要记得加上Exit要不然下一行程式执行的时候会当机 }

如何正确的终止正在运行的子线程

我的代码是这样的。。在多线程的子线程的调用方法里这样运行gethaha.exe,str1 是传递过来的参数。。Process process = new System.Diagnostics.Process();process.StartInfo.FileName = “gethaha.exe“;process.StartInfo.Arguments = str1;process.StartInfo.UseShellExecute = false; // 必须禁用操作系统外壳程序 process.StartInfo.CreateNoWindow = true;process.StartInfo.RedirectStandardOutput = true;process.EnableRaisingEvents = true;process.Start();System.IO.StreamReader reader = process.StandardOutput;//截取输出流string line = reader.ReadLine();//每次读取一行while (!reader.EndOfStream){ Console.WriteLine(line + “ “);line = reader.ReadLine();}process.WaitForExit(10000);if(process.HasExited ==false)process.Kill();reader.Close();我的意思是我想在每个线程里运行gethaha.exe 每个运行10秒钟如果10秒后它没有执行完毕就终止它问题1:可是运行中过了10秒后它并不终止,process.KIll() 没管用,而且我把主程序关了,gethaha.exe仍然在后台执行。到底是哪没有考虑到;问题2:还有那个重定向输出流,我想让它gethaha.exe输出一句Console就输出一句,现在的代码是gethaah.exe 执行完了,主程序把gethaah.exe输出的一次性全部输出出来。。。就这两个问题即使我再加上这两句代码也不顶用process.Dispose();Application.Exit();Thread.CurrentThread.Abort();它仍然在后台运行。。苦恼啊。。小弟初学c#,希望大牛指点迷津,不胜感激!!

C#怎在linux下怎么发起需要root权限的命令

使用sudo命令,命令格式sudo《需要root权限的具体command》sudo使一般用户不需要知道超级用户的密码即可获得权限。而可以使用sudo命令的用户则由/etc/sudoers配置文件来管理。编辑/etc/sudoers的方法:1.先给root用户/etc/sudoers文件的编辑权限,#chmod740/etc/sudoers2.#vi/etc/sudoers,进入编辑模式,找到这一行:“rootALL=(ALL)ALL“在起下面添加“xxxALL=(ALL)ALL“(这里的xxx是你的用户名),然后保存退出。3.把/etc/sudoers文件的权限复原,#chmod440/etc/sudoers使用sudo后,需要输入一次当前用户的密码进行身份验证(此处注意,并非root用户的密码!),五分钟后,再次使用sudo命令时,需要再次认证。命令实例:[mao@linux6~]$whoamimao[mao@linux6~]$sudowhoami[sudo]passwordformao:root[mao@linux6~]$

process_cmd.StartInfo.UseShellExecute = false;是什么意思啊

UseShellExecute must be true if you set the ErrorDialog property to true. The WorkingDirectory property behaves differently when UseShellExecute is true than when UseShellExecute is false. When UseShellExecute is true, the WorkingDirectory property specifies the location of the executable. If WorkingDirectory is an empty string, the current directory is understood to contain the executable.When UseShellExecute is false, the WorkingDirectory property is not used to find the executable. Instead, it is used by the process that is launched and only has meaning within the context of the new process.上面是MSDN原话,其实UseShellExecute 可以调用关联程序打开非可执行文件,如mp3;如果为false时,就是直接启动进程,只能执行可执行文件,必须指定完整路径,如exe和com文件,并且可以重定向标准输入与输出。

在C#中,那条语句是可以控制关机的

正好我以前没事的时候写着玩过一个.... using System.Diagnostics; //别忘了引这个命名空间 private static void QuXiaoGuanJi() //这个是取消关机的方法 { Process p = new Process(); p.StartInfo.FileName = “cmd.exe“; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(“shutdown -a“); p.StandardInput.WriteLine(“exit“); } private static void GuanJi() //这个是关机的方法 { Process p = new Process(); p.StartInfo.FileName = “cmd.exe“; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(“shutdown -s -t 90“); p.StandardInput.WriteLine(“exit“); } private void btnQueDing_Click(object sender, EventArgs e) //这是按钮的事件 { if (txtInput.Text == “我是猪“) { this.lblMessage.Text = “这样才对嘛....嘿嘿 爽!“; this.Refresh(); QuXiaoGuanJi(); Thread.Sleep(1000); Application.Exit(); } else { this.lblMessage.Text = “快点说哦~~ 时间不等人啊~~“; } }

怎样在CMD中获取所调用的.exe的返回值

ystem.Diagnostics.Process process = new System.Diagnostics.Process();process.StartInfo.FileName = “cmd.exe“;process.StartInfo.UseShellExecute = false;process.StartInfo.CreateNoWindow = true;process.StartInfo.RedirectStandardOutput = true;process.StartInfo.RedirectStandardInput = true;process.Start();process.StandardInput.WriteLine(strcms);process.StandardInput.AutoFlush = true;process.StandardInput.WriteLine(“exit“);process.WaitForExit();


声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,谢谢。

上一篇: 文件系统格式(如何知道自己文件系统格式是NTFS还是FAT32)

下一篇: 语文教育专业就业方向及就业前景分析(语文教育专业就业方向)



推荐阅读