2022年5月15日 星期日

[c#] process 使用方法

寫法1.

Process proc = new Process();

//PowerShell.exe path

proc.StartInfo.FileName = @"c:\Windows\System32\WindowsPowerShell\v1.0\

powershell.exe";


//取得window process,並且寫入process 
txt文字檔的PowerShell command

      proc.StartInfo.Arguments = @"Get-Process | Out-File c:\process.txt";

     proc.Start();

//Wait for the process to end.
      proc.WaitForExit();

//open process.txt

    StreamReader sr;

     sr = File.OpenText(@"c:\process.txt");

     string ProcessInfo = sr.ReadToEnd();

     sr.Close();

----------------------------------------------------------------------------------------------------------------
寫法2.
    Process currentProcess = Process.GetCurrentProcess();
    MessageBox.Show(currentProcess.Id.ToString());
--------------------------------------------------------------------------------------------------
寫法3.
     info.program = "catapult_s7_6g5_smt.pl";
    
     Process proc = Process.Start(info.program);

 if (proc != null)
 {
     proc.WaitForExit();

 }
--------------------------------------------------------------------------------------------------
//寫法4.讓程式自動啟動

   if (update_exe_file)
    {
        Process currentProcess = Process.GetCurrentProcess();

        // Run new program
        Process P_new = new Process();
        P_new.StartInfo = new ProcessStartInfo("cmd.exe", $"/C choice /C Y /N /D Y /T 1 & \"{APP_FullName}\" {currentProcess.Id.ToString()}");
        P_new.StartInfo.CreateNoWindow = true;
        P_new.StartInfo.UseShellExecute = false;
        P_new.Start();

        Application.Exit();
    }
}
catch (Exception)
{
    MessageBox.Show("無法連線伺服器, 請確認程式為最新版本 !!!");
    return false;
}

沒有留言:

[c#] process 使用方法

寫法1. Process proc = new Process(); / /PowerShell.exe path proc.StartInfo.FileName = @"c:\Windows\System32\ WindowsPowerShell\v1.0\ powe...