Caminhoneiro

Path examples

Jul 17th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.73 KB | None | 0 0
  1.  
  2.     [TestClass]
  3.     public class PathExample
  4.     {
  5.         [TestMethod]
  6.         public void CreatingPathManually()
  7.         {
  8.             var drive = "c:";
  9.             var dir = @"temp\pspathdemo\";
  10.             var file = "test.txt";
  11.  
  12.             var fullPath = drive;
  13.  
  14.             if (! drive.EndsWith(@"\"))
  15.             {
  16.                 fullPath += @"\";
  17.             }
  18.  
  19.             fullPath += dir;
  20.  
  21.             if (!dir.EndsWith(@"\"))
  22.             {
  23.                 fullPath += @"\";
  24.             }
  25.  
  26.             fullPath += file;
  27.         }
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.         [TestMethod]
  35.         public void UsingPathCombine()
  36.         {
  37.             var drive = "c:";
  38.             var dir = @"temp\pspathdemo\";
  39.             var file = "test.txt";
  40.  
  41.             var fullPath = System.IO.Path.Combine(drive, dir, file);
  42.            
  43.             drive = @"c:\";
  44.      
  45.             fullPath = Path.Combine(drive, dir, file);
  46.         }
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.         [TestMethod]
  56.         public void UsefulPathMethods()
  57.         {
  58.             var path = @"c:\temp\pspathdemo\test.txt";
  59.  
  60.             path = Path.ChangeExtension(path, "bak");
  61.  
  62.             var dirName = Path.GetDirectoryName(path);
  63.  
  64.             var ext = Path.GetExtension(path);
  65.  
  66.             var file = Path.GetFileName(path);
  67.            
  68.             var fileNoExt = Path.GetFileNameWithoutExtension(path);
  69.  
  70.             bool hasExt = Path.HasExtension(path);
  71.         }
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.         [TestMethod]
  80.         public void UsefulGeneralMethods()
  81.         {
  82.             var invalidNameChars = Path.GetInvalidFileNameChars();
  83.  
  84.             var rndFileName = Path.GetRandomFileName();
  85.  
  86.             var rndTempFile = Path.GetTempFileName();
  87.  
  88.             var userTempPath = Path.GetTempPath();
  89.  
  90.             char platformSpecificDirSeparater = Path.DirectorySeparatorChar;
  91.         }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.         [TestMethod]
  101.         public void PathCombinePeculiarities()
  102.         {
  103.             // combining "absolute" paths
  104.             var result = Path.Combine(@"\data", @"c:\temp");
  105.  
  106.             result = Path.Combine(@"c:\temp", @"\data"); //  \data is considered an absolute path
  107.  
  108.             result = Path.Combine(@"c:\temp", @"data");
  109.  
  110.             result = Path.Combine(@"c:\temp", @"\data".TrimStart(Path.DirectorySeparatorChar));
  111.  
  112.  
  113.  
  114.             // empty strings
  115.             result = Path.Combine("", @"c:\data");
  116.             result = Path.Combine(@"x:\data", "");
  117.  
  118.  
  119.  
  120.             // using ".." to refer to parent dir
  121.             result = Path.Combine(@"c:\temp\data", @"..");
  122.            
  123.            
  124.             result = Path.GetFullPath(result);
  125.             // if path actually exists we must have permission to obtain path info for it          
  126.         }
  127.  
  128.     }
Advertisement
Add Comment
Please, Sign In to add comment