Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

วิธีรันแอปพลิเคชันภายนอกผ่านแอปพลิเคชัน C#

แอปพลิเคชันภายนอกสามารถรันได้จากแอปพลิเคชัน C# โดยใช้คลาส Process Process คือโปรแกรมที่กำลังรันบนคอมพิวเตอร์ของคุณ สิ่งนี้อาจเป็นงานเบื้องหลังเล็กๆ เช่น Spell-checker หรือ Event Handler จนถึงแอปพลิเคชันเต็มรูปแบบอย่าง Notepad ฯลฯ

แต่ละ Process จัดเตรียมทรัพยากรที่จำเป็นสำหรับการ Execute โปรแกรม แต่ละ Process เริ่มต้นด้วย Thread เดียว ซึ่งเรียกว่า Primary Thread Process หนึ่งสามารถมี Multiple Threads เพิ่มเติมจาก Primary Thread ได้ Process ขึ้นอยู่กับ System Resources มาก ในขณะที่ Threads ต้องการทรัพยากรน้อยมาก ดังนั้น Process ถือว่าเป็น Heavyweight ในขณะที่ Thread เรียกว่า Lightweight Process คลาส Process อยู่ใน Namespace System.Diagnostics

ตัวอย่าง: รัน Notepad จากแอปพลิเคชัน C#

using System;
using System.Diagnostics;

namespace DemoApplication {
    class Program {
        static void Main() {
            Process notepad = new Process();
            notepad.StartInfo.FileName = "notepad.exe";
            notepad.StartInfo.Arguments = "DemoText.txt";
            notepad.Start();
            Console.ReadLine();
        }
    }
}

โค้ดด้านบนจะเปิด Notepad พร้อมโหลดไฟล์ DemoText.txt (ถ้ามีอยู่) หรือสร้างไฟล์ใหม่ชื่อ DemoText.txt

ตัวอย่าง: เปิด Browser และไปยัง URL

using System;
using System.Diagnostics;

namespace DemoApplication {
    class Program {
        static void Main() {
            // ใช้ Start แบบ Static โดยตรง
            Process.Start("https://www.google.com/");
            Console.ReadLine();
        }
    }
}

โค้ดด้านบนจะเปิด Browser เริ่มต้นและ Redirect ไปยัง www.google.com

ตัวอย่างขั้นสูง: รัน Process พร้อมจับ Output

using System;
using System.Diagnostics;

namespace DemoApplication {
    class Program {
        static void Main() {
            var process = new Process {
                StartInfo = new ProcessStartInfo {
                    FileName = "cmd.exe",
                    Arguments = "/c dir",
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                }
            };
            
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();
            process.WaitForExit();
            
            Console.WriteLine("Output:");
            Console.WriteLine(output);
            
            if (!string.IsNullOrEmpty(error)) {
                Console.WriteLine("Error:");
                Console.WriteLine(error);
            }
            
            Console.WriteLine($"Exit Code: {process.ExitCode}");
            Console.ReadLine();
        }
    }
}

คุณสมบัติสำคัญของ ProcessStartInfo

  • FileName - ชื่อไฟล์ Executable หรือไฟล์ที่เกี่ยวข้อง
  • Arguments - อาร์กิวเมนต์สำหรับโปรแกรม
  • WorkingDirectory - โฟลเดอร์ทำงาน
  • RedirectStandardOutput/Error/Input - เปลี่ยนทิศทาง I/O
  • UseShellExecute - ต้องเป็น false ถ้าต้องการ Redirect I/O
  • CreateNoWindow - ไม่สร้างหน้าต่าง Console (สำหรับ GUI apps)
  • Verb - เช่น "runas" เพื่อรันเป็น Administrator