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

      

  • 相关阅读:
    [Web安全] XXE漏洞攻防学习(中)
    [Web安全] XXE漏洞攻防学习(上)
    [转]kali中eth0网卡突然消失解决方案
    [漏洞复现]CVE-2018-4887 Flash 0day
    [漏洞复现]CVE-2010-2883 Adobe Reader 打开pdf电脑即刻中招
    [漏洞复现] CVE-2017-11882 通杀所有Office版本
    墨菲定律:Mac本硬盘坏了
    独立思考
    阅读书单
    2020未来小思考
  • 原文地址:https://www.cnblogs.com/haimingkaifa/p/5765793.html
Copyright © 2011-2022 走看看