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

      

  • 相关阅读:
    [BZOJ2809][Apio2012]dispatching
    [BZOJ4584][Apio2016]赛艇
    [BZOJ3206][Apio2013]道路费用
    [codeforces551E]GukiZ and GukiZiana
    [BZOJ3809]Gty的二逼妹子序列
    [BZOJ3289]Mato的文件管理
    [BZOJ3052][UOJ#58][WC2013]糖果公园
    [SPOJ10707]Count on a tree II
    [BZOJ1086][SCOI2005]王室联邦
    python小知识
  • 原文地址:https://www.cnblogs.com/haimingkaifa/p/5765793.html
Copyright © 2011-2022 走看看