进程系列(三)-进程的基本用法(打开文件示列)

本文介绍了一个简单的文件操作框架设计,包括一个基类和三个子类(TXT、PDF、XLS),并通过简单工厂模式实现了不同文件类型的实例化及打开功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步   写一个文件父类:

 public class BaseFile
    {
        public string FilePath { get; set; }

        private string _fileName;

        public string FileName
        {
            get { return _fileName; }
            set { _fileName = value; }
        }
        public BaseFile() 
        {
        
        }
        public BaseFile(string filePath,string fileName)
        {
            this.FilePath = filePath;
            this.FileName = fileName;
        }

        /// <summary>
        /// 设计一个函数用来打开指定的函数
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        public void OpenFile(string filePath, string fileName) 
        {
            ProcessStartInfo psi = new ProcessStartInfo(filePath+"//"+fileName);

            Process pro = new Process();

            pro.StartInfo = psi;

            pro.Start();
        }
    }

 

第二步   分别新建一个TXT,PDF,XLS文件子类继承文件父类:

TXT:

public class TextFile : BaseFile
    {
        public TextFile(string filePath, string fileName)
            : base(filePath,fileName)
        {

        }
    }

PDF:

public class PdfFile : BaseFile
    {
        public PdfFile(string filePath, string fileName)
            : base(filePath, fileName)
        {

        }
    }

XLS:

 public class XlsFile : BaseFile
    {
        public XlsFile(string filePath, string fileName)
            : base(filePath, fileName)
        {

        }
    }

 

第三步 通过简单工厂模式返回设计父类:

 public class Common
    {
        public static BaseFile GetFile(string filePath,string fileName) 
        {
            string strExtension = Path.GetExtension(fileName);

            BaseFile bf = null;

            switch (strExtension)
            {
                case ".txt":
                    bf = new TextFile(filePath, fileName);
                    break;
                case ".pdf":
                    bf = new PdfFile(filePath, fileName);
                    break;
                case ".xls":
                    bf = new XlsFile(filePath, fileName);
                    break;
            }

            return bf;
        }
    }

 

第四步   从控制台操作要打开的文件:

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你要打开文件的路径:");
            string filePath = Console.ReadLine();
            Console.WriteLine("请输入你要开文件的名字:");
            string fileName = Console.ReadLine();

            //通过简单工厂模式返回设计父类
            BaseFile bf = Common.GetFile(filePath, fileName);

            if (bf!=null)
            {
                bf.OpenFile(filePath, fileName);
            }

            Console.ReadKey();
        }
    }

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神毓逍遥-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值