zoukankan      html  css  js  c++  java
  • 打开文件

        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;
            }
    

      

  • 相关阅读:
    Doubles
    The 3n + 1 problem
    Counterfeit Dollar
    Maya Calendar
    08_python的列表、元祖、字符串、字典及公共方法
    07_python的函数
    06_python的while语句
    05_python的if语句
    04_python基本的输入输出函数
    03_python的数据类型和变量的定义及使用
  • 原文地址:https://www.cnblogs.com/haimingkaifa/p/5765793.html
Copyright © 2011-2022 走看看