alt+shift+f10 调出 添加 Process的引用
Process不是静态类,但有静态成员
Process[] pro = Process.GetProcesses();
获取所有进程(数组)
使用进程打开文件
Process.Start("notepad"); Process.Start("mspaint"); Process.Start("iexplore", "http://baidu.com"); Process.Start("calc"); Console.ReadKey();
//封装我们要打开的文件的相关信息,但并不打开 ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\andu\Desktop\a.txt"); Process pro = new Process(); //告诉进行去打开 pro.StartInfo = psi; //这里的start(),是实例函数,非静态函数 pro.Start();
--------------------------------------------------------------
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _01进程{ class Program { static void Main(string[] args) { //Process[] pro = Process.GetProcesses(); //foreach (var item in pro) //{ // item.Kill(); // Console.WriteLine(item.ProcessName); //} //Process.Start("notepad"); //Process.Start("mspaint"); //Process.Start("iexplore", "http://baidu.com"); //Process.Start("calc"); ////封装我们要打开的文件的相关信息,但并不打开 //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\andu\Desktop\a.txt"); //Process pro = new Process(); ////告诉进行去打开 //pro.StartInfo = psi; ////这里的start(),是实例函数,非静态函数 //pro.Start(); Console.WriteLine("请输入打开文件路径"); string filePath = Console.ReadLine(); Console.WriteLine("请输入文件名"); string fileName = Console.ReadLine(); //通过简单工厂设计模式返回父类 BaseFile bf = GetFile(filePath, fileName); if (bf!=null) { bf.OpenFile(); } Console.ReadKey(); } static BaseFile GetFile(string filePath,string fileName) { BaseFile bf = null; string strExtension = Path.GetExtension(fileName); // 3.txt switch (strExtension) { case ".txt":bf = new TxtFile(filePath, fileName); break; case ".avi":bf= new AviFile(filePath, fileName); break; case ".mp4":bf = new Mp4File(filePath, fileName); break; default: break; } return bf; } } class BaseFile { //字段、属性、构造函数、函数、索引器 private string _filePath; //封装属性快捷键 ctrl+r+e public string FilePath { get { return _filePath; } set { _filePath = value; } } //自动属性 快捷键 prop + 两下 tab //属性的本质:两个函数,get和set public string FileName { get; set; } public BaseFile(string filePath,string fileName) { this.FilePath = filePath; this.FileName = fileName; } //设计一个函数,打开指定文件 public void OpenFile() { ProcessStartInfo psi = new ProcessStartInfo(this.FilePath + "\\" + this.FileName); Process pro = new Process(); pro.StartInfo = psi; pro.Start(); } } class TxtFile : BaseFile { //子类默认调用父类无参构造函数,父类有参构造函数替换了无参的构造函数 public TxtFile(string filePath,string fileName) : base(filePath, fileName) { } } class Mp4File : BaseFile { public Mp4File(string filePath,string fileName) : base(filePath, fileName) { } } class AviFile :BaseFile { public AviFile(string filePath,string fileName) : base(filePath, fileName) { } }}