c#基础学习(0628)之使用进程打开指定的文件、模拟磁盘打开文件

使用进程打开指定的文件
View Code

 

模拟磁盘打开文件

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();
  }
}
    
View Code

 

posted @ 2018-06-28 23:58  超哥20  阅读(334)  评论(0编辑  收藏  举报