설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileIO.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.IO;
  3. using System.Security;
  4. using System.Text;
  5. namespace VSCodeEditor
  6. {
  7. public interface IFileIO
  8. {
  9. bool Exists(string fileName);
  10. string ReadAllText(string fileName);
  11. void WriteAllText(string fileName, string content);
  12. void CreateDirectory(string pathName);
  13. string EscapedRelativePathFor(string file, string projectDirectory);
  14. }
  15. class FileIOProvider : IFileIO
  16. {
  17. public bool Exists(string fileName)
  18. {
  19. return File.Exists(fileName);
  20. }
  21. public string ReadAllText(string fileName)
  22. {
  23. return File.ReadAllText(fileName);
  24. }
  25. public void WriteAllText(string fileName, string content)
  26. {
  27. File.WriteAllText(fileName, content, Encoding.UTF8);
  28. }
  29. public void CreateDirectory(string pathName)
  30. {
  31. Directory.CreateDirectory(pathName);
  32. }
  33. public string EscapedRelativePathFor(string file, string projectDirectory)
  34. {
  35. var projectDir = Path.GetFullPath(projectDirectory);
  36. // We have to normalize the path, because the PackageManagerRemapper assumes
  37. // dir seperators will be os specific.
  38. var absolutePath = Path.GetFullPath(file.NormalizePath());
  39. var path = SkipPathPrefix(absolutePath, projectDir);
  40. return SecurityElement.Escape(path);
  41. }
  42. private static string SkipPathPrefix(string path, string prefix)
  43. {
  44. return path.StartsWith($@"{prefix}{Path.DirectorySeparatorChar}", StringComparison.Ordinal)
  45. ? path.Substring(prefix.Length + 1)
  46. : path;
  47. }
  48. }
  49. }