使用进程打开指定文件的实现步骤:
Console.WriteLine("使用进程打开指定的文件:"+"………………………………");
//1.封装我们要打开的文件
string path = "E:\\2018C#Learn\\Projects\\ConsoleApp2\\WindowsFormsApp1\\bin\\Debug\\WindowsFormsApp1.exe";
ProcessStartInfo startInfo = new ProcessStartInfo(path);
//2.创建进程对象
Process pro = new Process();
//3.告诉进程要打开的文件信息
pro.StartInfo = startInfo;
//4.实例函数调用方法
pro.Start();
案例完整版实现代码:
satic void Main(String args)静态方法:
//静态方法
static BaseFile GetFile(string filePath, string fileName)
{
//把指定的父类文件名字返回出去
BaseFile bf = null;
//获取文件的后缀名字(.txt)
string strExtension = Path.GetExtension(fileName);
//根据名字来判断(生成不同的实例对象)
switch (strExtension)
{
case ".txt":
bf = new TxtFile(filePath, fileName);
break;
case ".mp4":
bf = new Mp4File(filePath, fileName);
break;
case ".avi":
bf = new AviFile(filePath, fileName);
break;
default:
Console.WriteLine("当前系统暂不支持该文件格式!");
break;
}
return bf;
}
}
文件的读写项目:
class BaseFile
{
//字段, 属性, 构造函数, 函数, 索引器
private string _filePath;
//封装属性的快捷键(Ctrl+R+E)
public string FilePath
{
get => _filePath;
set => _filePath = value;
}
//自动属性的快捷键:prop+两下tab 按键
public string FileName { get; set; }
//构造函数(有参)
public BaseFile(string filePath, string fileName)
{
this.FileName = fileName;
this.FilePath = filePath;
}
//无参构造函数
public BaseFile()
{
}
//设计一个函数,用来打开指定的文件
public void OpenFile()
{
ProcessStartInfo psi = new ProcessStartInfo(this.FilePath+"\\"+this.FileName);
Process pro = new Process();
pro.StartInfo = psi;
pro.Start();
}
}
//子类的设计(继承积累)
class TxtFile : BaseFile
{
//子类默认会调用 父类的无参构造函数; 因此父类必须要有无参构造函数【错误点1】
//让子类显式的去调用父类的有参构造函数
public TxtFile(string filePath, string fileName) : base(filePath, fileName)
{
}
}
//这个类继承父类,也具有父类的方法和属性
class Mp4File : BaseFile
{
public Mp4File(string filePath, string fileName):base(filePath, fileName)
{
}
}
class AviFile : BaseFile
{
//子类的有参构造函数
public AviFile(string filePath, string fileName):base(filePath, fileName)
{
}
}
}
评论
填写昵称与邮箱即可评论,无需登录。