实现模拟硬盘打开文件:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _129_复习_模拟硬盘打开文件
{
class Program
{
static void Main(string[] args)
{
////使用进程打开指定文件
//ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\46124\Desktop\CSharpTestTwo.mp4");
//Process p = new Process();
//p.StartInfo = psi;
//p.Start();
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 ".mp4":
ff = new Mp4Path(fullPath);
break;
}
return ff;
}
}
}
FileFather.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _129_复习_模拟硬盘打开文件
{
public abstract class FileFather
{
public string fullPath
{
get;
set;
}
public FileFather(string fullPath)
{
this.fullPath = fullPath;
}
public abstract void OpenFile();
}
}
TxtPath:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _129_复习_模拟硬盘打开文件
{
class TxtPath : FileFather
{
public TxtPath(string fullPath) : base(fullPath)
{
}
public override void OpenFile()
{
ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
Process p = new Process();
p.StartInfo = psi;
p.Start();
}
}
}
JpgPath.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace _129_复习_模拟硬盘打开文件
{
class JpgPath : FileFather
{
public JpgPath(string fullPath) : base(fullPath)
{ }
public override void OpenFile()
{
ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
Process p = new Process();
p.StartInfo = psi;
p.Start();
}
}
}
Mp4Path.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace _129_复习_模拟硬盘打开文件
{
class Mp4Path : FileFather
{
public Mp4Path(string fullPath) : base(fullPath)
{ }
public override void OpenFile()
{
ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
Process p = new Process();
p.StartInfo = psi;
p.Start();
}
}
}