标签:
public class MyFile { /// <summary> /// 定义属性获取文件路径 /// </summary> public string PathFileName { get; set; } public MyFile() { } /// <summary> /// 构造函数初始化值 /// </summary> /// <param name="pathFileName">文件路径</param> public MyFile(string pathFileName) { this.PathFileName = pathFileName; } public void OpenFile() //打开文件的进程方法 { ProcessStartInfo pro = new ProcessStartInfo(PathFileName);//启动进程获取文件路径 Process pr = new Process();//开启进程 pr.StartInfo = pro;//赋值 pr.Start();//打开 } } class TxtFile : MyFile { public TxtFile(string pathFileName) : base(pathFileName) { } } }
private void button1_Click(object sender, EventArgs e) { string fileName; OpenFileDialog op = new OpenFileDialog();//打开对话框 op.ShowDialog(); fileName = op.FileName;//获取文件名 textBox1.Text = fileName; string fileExtension= Path.GetExtension(fileName);//获取扩展名 //打开 MyFile my = new MyFile(fileName); try { my = OpenFileStr(fileExtension, fileName); my.OpenFile(); } catch { MessageBox.Show("输入的格式有误"); textBox1.Text = null; } } /// <summary> /// 打开文件的方法 /// </summary> /// <param name="fileExtension">扩展名</param> /// <param name="fileName">文件路径</param> /// <returns></returns> public MyFile OpenFileStr(string fileExtension,string fileName)// { MyFile my = null; switch (fileExtension) { case ".txt": my = new TxtFile(fileName); break; } return my; }
标签:
原文地址:http://www.cnblogs.com/haimingkaifa/p/5765793.html