
正文
c#基础学习(0628)之使用进程打开指定的文件、模拟磁盘打开文件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
使用进程打开指定的文件
模拟磁盘打开文件
class Program
{
static void Main(string[] args)
{
while(true)
{
Console.WriteLine("请选择要进入的磁盘");
string path=Console.ReadLine();//D:\
Console.WriteLine("请选择要打开的文件");
string fileName=Console.ReadLine();//1.txt
//文件的全路径:path+fileName
FileFather ff=GetFile(fileName,path+fileName);
ff.OpenFile();
Console.ReadKey();
}
}
}
public static FileFather GetFile(string fileName,string fullPath)
{
string extension=Path.GetExtension(fileName);
FileFather ff=null;
switch(extension)
{
case ".txt":ff=new TxtPath(fullPath);
break;
case ".jpg":ff=new JpgPath(fullPath);
break;
case ".wmv":ff=new WmvPath(fullPath);
break;
}
}
public abstract class FileFather
{
public string fullPath
{
get;
set;
}
public FileFather(string fullPath)
{
this.fullPath=fullPath;
}
public abstract void OpenFile();
}
public class TxtPath:FileFather
{
public TxtPath(string fullPath):base(fullPath)
{ }
public override void OpenFile()
{
ProcessStartInfo psi=new ProcessStartInfo(this.fileName);
Process p=new Process();
p.StartInfo=psi;
p.Start();
}
}
public class JpgPath:FileFather
{
public JpgPath(string fullPath):base(fullPath)
{ }
public override void OpenFile()
{
ProcessStartInfo psi=new ProcessStartInfo(this.fileName);
Process p=new Process();
p.StartInfo=psi;
p.Start();
}
}
public class WmvPath:FileFather
{
public WmvPath(string fullPath):base(fullPath)
{ }
public override void OpenFile()
{
ProcessStartInfo psi=new ProcessStartInfo(this.fileName);
Process p=new Process();
p.StartInfo=psi;
p.Start();
}
}






