User Tools

Site Tools


notes:csharp:io

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
notes:csharp:io [2018/10/22]
leszek [I/O and Streams in .NET]
notes:csharp:io [2018/10/22] (current)
leszek
Line 415: Line 415:
  
  
-====Path==== +====File====
- +
-Example: Combine a path and a file name: +
-<code csharp>​ +
-string fullPath = Path.Combine(@"​C:​\Temp",​ "​data.dbf"​);​ +
-</​code>​ +
- +
-Example: Show sections of a path. +
-<code csharp>​ +
-string path = @"​C:​\Temp\MyFolder\MyFile.txt";​ +
-Console.WriteLine(Path.GetDirectoryName(path));​ // C:​\Temp\MyFolder +
-Console.WriteLine(Path.GetExtension(path)); ​    // .txt +
-Console.WriteLine(Path.GetFileName(path)); ​     // data.dbf +
-Console.WriteLine(Path.GetPathRoot(path)); ​     // C:\ +
-</​code>​ +
- +
- +
-Example: Obtain random files and folders: +
-<code csharp>​ +
-// Obtain a random file name. +
-string name = Path.GetRandomFileName();​ +
- +
-// Create a unique, temporary file in the user\AppData\Local\Temp folder and return the full path to that file. +
-string tmpFile = Path.GetTempFileName();​ +
- +
-// Obtain the path of the current user's temporary folder. +
-string tmpFolder = Path.GetTempPath();​ +
-</​code>​ +
- +
- +
- +
- +
-====Zip Files==== +
- +
-You can use the //​ZipArchive//​ and //ZipFile// classes to compress files in zip format. The classes are located in the //​System.IO.Compression//​ namespace in the //​System.IO.Compression.dll//​ assembly. This feature was introduced in .NET 4.5. +
- +
-Example: Zip all the files in a specified folder: +
-<code csharp>​ +
-ZipFile.CreateFromDirectory (@"​C:​\Documents",​ @"​C:​\Temp\docs.zip"​);​ +
-</​code>​ +
- +
-Example: Extract all the files from a zip file to a folder: +
-<code csharp>​ +
-ZipFile.ExtractToDirectory (@"​C:​\Temp\docs.zip",​ @"​C:​\Documents"​);​ +
-</​code>​ +
- +
- +
- +
- +
-====File ​/ FileInfo====+
  
 Use static methods of the //File// class or instance methods of the //​FileInfo//​ class. Use static methods of the //File// class or instance methods of the //​FileInfo//​ class.
Line 547: Line 498:
  
  
-====Directory ​/ DirectoryInfo====+====Directory====
  
 Use static methods of the //​Directory//​ class or instance methods of the //​DirectoryInfo//​ class. Use static methods of the //​Directory//​ class or instance methods of the //​DirectoryInfo//​ class.
Line 630: Line 581:
  
  
 +====Path====
 +
 +Example: Combine a path and a file name:
 +<code csharp>
 +string fullPath = Path.Combine(@"​C:​\Temp",​ "​data.dbf"​);​
 +</​code>​
 +
 +Example: Show sections of a path.
 +<code csharp>
 +string path = @"​C:​\Temp\MyFolder\MyFile.txt";​
 +Console.WriteLine(Path.GetDirectoryName(path));​ // C:​\Temp\MyFolder
 +Console.WriteLine(Path.GetExtension(path)); ​    // .txt
 +Console.WriteLine(Path.GetFileName(path)); ​     // data.dbf
 +Console.WriteLine(Path.GetPathRoot(path)); ​     // C:\
 +</​code>​
 +
 +
 +Example: Obtain random files and folders:
 +<code csharp>
 +// Obtain a random file name.
 +string name = Path.GetRandomFileName();​
 +
 +// Create a unique, temporary file in the user\AppData\Local\Temp folder and return the full path to that file.
 +string tmpFile = Path.GetTempFileName();​
 +
 +// Obtain the path of the current user's temporary folder.
 +string tmpFolder = Path.GetTempPath();​
 +</​code>​
 +
 +
 +
 +
 +====Zip Files====
 +
 +You can use the //​ZipArchive//​ and //ZipFile// classes to compress files in zip format. The classes are located in the //​System.IO.Compression//​ namespace in the //​System.IO.Compression.dll//​ assembly. This feature was introduced in .NET 4.5.
 +
 +Example: Zip all the files in a specified folder:
 +<code csharp>
 +ZipFile.CreateFromDirectory (@"​C:​\Documents",​ @"​C:​\Temp\docs.zip"​);​
 +</​code>​
 +
 +Example: Extract all the files from a zip file to a folder:
 +<code csharp>
 +ZipFile.ExtractToDirectory (@"​C:​\Temp\docs.zip",​ @"​C:​\Documents"​);​
 +</​code>​
  
  
notes/csharp/io.txt · Last modified: 2018/10/22 by leszek