image.png

    实现模拟硬盘打开文件:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.IO;
    5. using System.Linq;
    6. using System.Text;
    7. using System.Threading.Tasks;
    8. namespace _129_复习_模拟硬盘打开文件
    9. {
    10. class Program
    11. {
    12. static void Main(string[] args)
    13. {
    14. ////使用进程打开指定文件
    15. //ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\46124\Desktop\CSharpTestTwo.mp4");
    16. //Process p = new Process();
    17. //p.StartInfo = psi;
    18. //p.Start();
    19. Console.WriteLine("请选择要进入的磁盘:");
    20. string path = Console.ReadLine(); //D:
    21. Console.WriteLine("请选择要打开的文件:");
    22. string fileName = Console.ReadLine(); //\1.txt
    23. //文件的全路径:path+fileName
    24. FileFather ff = GetFile(fileName, path + fileName);
    25. ff.OpenFile();
    26. Console.ReadKey();
    27. }
    28. public static FileFather GetFile(string fileName, string fullPath)
    29. {
    30. string extension = Path.GetExtension(fileName);
    31. FileFather ff = null;
    32. switch (extension)
    33. {
    34. case ".txt":
    35. ff = new TxtPath(fullPath);
    36. break;
    37. case ".jpg":
    38. ff = new JpgPath(fullPath);
    39. break;
    40. case ".mp4":
    41. ff = new Mp4Path(fullPath);
    42. break;
    43. }
    44. return ff;
    45. }
    46. }
    47. }

    FileFather.cs

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. namespace _129_复习_模拟硬盘打开文件
    7. {
    8. public abstract class FileFather
    9. {
    10. public string fullPath
    11. {
    12. get;
    13. set;
    14. }
    15. public FileFather(string fullPath)
    16. {
    17. this.fullPath = fullPath;
    18. }
    19. public abstract void OpenFile();
    20. }
    21. }

    TxtPath:

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Diagnostics;
    4. using System.Linq;
    5. using System.Text;
    6. using System.Threading.Tasks;
    7. namespace _129_复习_模拟硬盘打开文件
    8. {
    9. class TxtPath : FileFather
    10. {
    11. public TxtPath(string fullPath) : base(fullPath)
    12. {
    13. }
    14. public override void OpenFile()
    15. {
    16. ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
    17. Process p = new Process();
    18. p.StartInfo = psi;
    19. p.Start();
    20. }
    21. }
    22. }

    JpgPath.cs

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Diagnostics;
    7. namespace _129_复习_模拟硬盘打开文件
    8. {
    9. class JpgPath : FileFather
    10. {
    11. public JpgPath(string fullPath) : base(fullPath)
    12. { }
    13. public override void OpenFile()
    14. {
    15. ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
    16. Process p = new Process();
    17. p.StartInfo = psi;
    18. p.Start();
    19. }
    20. }
    21. }

    Mp4Path.cs

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6. using System.Diagnostics;
    7. namespace _129_复习_模拟硬盘打开文件
    8. {
    9. class Mp4Path : FileFather
    10. {
    11. public Mp4Path(string fullPath) : base(fullPath)
    12. { }
    13. public override void OpenFile()
    14. {
    15. ProcessStartInfo psi = new ProcessStartInfo(this.fullPath);
    16. Process p = new Process();
    17. p.StartInfo = psi;
    18. p.Start();
    19. }
    20. }
    21. }