標籤:
static void Main(string[] args) { //使用進程開啟指定檔案 ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\Adobe註冊機使用說明.txt"); Process p = new Process(); p.StartInfo = psi; p.Start(); }
會和前面的筆記重複,但是還是複習一遍吧,上次就沒太整明白
類比控制台開啟檔案:(複習物件導向)
1、在控制台提示使用者要進入的磁碟路徑
D:\
2、提示使用者輸入要開啟的檔案的名稱
1.txt
這樣就獲得了D:\1.txt 這是檔案的全路徑
父類:
public abstract class FileFather { public string FileName { get; set; } public FileFather(string fileName) { this.FileName = fileName; } public abstract void OpenFile(); }}
TxtPath子類:
class TxtPath : FileFather { public TxtPath(string fileName) : base(fileName) { } public override void OpenFile() { ProcessStartInfo psi = new ProcessStartInfo(this.FileName); Process p = new Process(); p.StartInfo = psi; p.Start(); } }
JpgPath子類:
class JpgPath : FileFather { public JpgPath(string fileName) : base(fileName) { } public override void OpenFile() { ProcessStartInfo psi = new ProcessStartInfo(this.FileName); Process p = new Process(); p.StartInfo = psi; p.Start(); } }
WmvPath子類:
class WmvPath : FileFather { public WmvPath(string fileName) : base(fileName) { } public override void OpenFile() { ProcessStartInfo psi = new ProcessStartInfo(this.FileName); Process p = new Process(); p.StartInfo = psi; p.Start(); } }
程式入口:
static void Main(string[] args) { //使用進程開啟指定檔案 //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\Adobe註冊機使用說明.txt"); //Process p = new Process(); //p.StartInfo = psi; //p.Start(); Console.WriteLine("請選擇要進入的磁碟"); string path = Console.ReadLine(); Console.WriteLine("請選擇要開啟的檔案"); string fileName = Console.ReadLine(); //檔案全路徑 = path + fileName FileFather ff = GetFile(fileName,path + fileName); ff.OpenFile(); Console.ReadKey(); } public static FileFather GetFile(string fileName,string fullPath) { string extension = Path.GetExtension(fileName); FileFather ff = null; switch(extension) { case ".txt": ff = new TxtPath(fullPath); break; case ".jpg": ff = new JpgPath(fullPath); break; case ".wmv": ff = new WmvPath(fullPath); break; } return ff; }
代碼冗餘,主要為練習抽象類別和抽象方法
.Net學習筆記----2015-07-21(C#基礎複習03,簡單工廠和抽象類別)