You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/develop/files/dotnet-files.md
+130Lines changed: 130 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,10 +13,140 @@ Packaged Windows App SDK apps can leverage .NET APIs for reading and writing fil
13
13
14
14
## Read and write files with .NET APIs
15
15
16
+
In the following example, `ReadWriteFiles` creates a new file, writes a set of integers to the file, and then reads the integers back from the file. The example uses the [FileStream](/dotnet/api/system.io.filestream) class to create a new file and to open the file for reading or writing. The example uses the [BinaryWriter](/dotnet/api/system.io.binarywriter) class to write the integers to the file and the [BinaryReader](/dotnet/api/system.io.binaryreader) class to read the integers from the file.
17
+
18
+
```csharp
19
+
usingSystem.IO;
20
+
...
21
+
ReadWriteFiles("test.bin");
22
+
...
23
+
privatevoidReadWriteFiles(stringfileName)
24
+
{
25
+
if (File.Exists(fileName))
26
+
{
27
+
Console.WriteLine($"{fileName} already exists!");
28
+
return;
29
+
}
30
+
31
+
using (FileStreamfs=new(fileName, FileMode.CreateNew))
32
+
{
33
+
usingBinaryWriterwriter=new(fs);
34
+
for (inti=0; i<11; i++)
35
+
{
36
+
writer.Write(i);
37
+
}
38
+
}
39
+
40
+
using (FileStreamfs=new(fileName, FileMode.Open, FileAccess.Read))
41
+
{
42
+
usingBinaryReaderreader=new(fs);
43
+
for (inti=0; i<11; i++)
44
+
{
45
+
Console.WriteLine(reader.ReadInt32());
46
+
}
47
+
}
48
+
}
49
+
```
50
+
16
51
## Manage drives and folders in .NET
17
52
53
+
The following example shows how to use the [DirectoryInfo](/dotnet/api/system.io.directoryinfo) and [Directory](/dotnet/api/system.io.directory) classes to create, delete, and manage folders. The example uses the `DirectoryInfo` class to create a new directory, create a subdirectory, and delete the directory. The `DirectoryInfo` class provides methods for creating, moving, and enumerating through directories and subdirectories. The `Directory` class provides *static* methods for creating, moving, and enumerating through directories and subdirectories.
Console.WriteLine("The subdirectory was created successfully.");
88
+
}
89
+
90
+
// Delete directory
91
+
di.Delete(true);
92
+
Console.WriteLine("The directory was deleted successfully.");
93
+
}
94
+
catch (Exceptionex)
95
+
{
96
+
Console.WriteLine("The process failed: {0}", ex.ToString());
97
+
}
98
+
}
99
+
```
100
+
101
+
This example using the static [GetDrives](/dotnet/api/system.io.driveinfo.getdrives) method to retrieve information about all drives on the system. The [DriveInfo](/dotnet/api/system.io.driveinfo) class provides information about a drive, such as the drive type, label, file system, and available free space.
Console.WriteLine($" File system type: {d.DriveFormat}");
118
+
Console.WriteLine($" Space available to user: {d.AvailableFreeSpace, 15} bytes");
119
+
Console.WriteLine($" Total available space: {d.TotalFreeSpace, 15} bytes");
120
+
Console.WriteLine($" Total size of drive: {d.TotalSize, 15} bytes ");
121
+
}
122
+
}
123
+
}
124
+
```
125
+
18
126
## Encode and decode strings with MemoryStream
19
127
128
+
This example shows how to use the [MemoryStream](/dotnet/api/system.io.memorystream) class to encode and decode string data. It first creates a `MemoryStream` to asynchronously write a string to a memory stream and then read the string from the memory stream. The [Encoding](/dotnet/api/system.text.encoding) class is used to convert the string to a byte array and then write the byte array to the memory stream. A [StreamReader](/dotnet/api/system.io.streamreader) is then used to asynchronously read the byte array from the memory stream and then convert the byte array back to a string by calling [ReadToEndAsync](/dotnet/api/system.io.streamreader.readtoendasync).
> For informatio about converting between .NET streams and WinRT streams, see [How to: Convert between .NET and Windows Runtime streams](/dotnet/standard/io/how-to-convert-between-dotnet-streams-and-winrt-streams).
149
+
20
150
## See also
21
151
22
152
[Access files and folders with Windows App SDK and WinRT APIs](winrt-files.md)
Copy file name to clipboardExpand all lines: hub/apps/develop/files/winrt-files.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,6 +16,7 @@ Packaged Windows App SDK apps can leverage [WinRT APIs](/uwp/api/) for reading a
16
16
The following example shows how to use the [StorageFolder](/uwp/api/windows.storage.storagefolder) and [StorageFile](/uwp/api/windows.storage.storagefile) APIs to query the **Documents** library for files and folders. The example uses the `GetFilesInFolderAsync` method to recursively iterate through the folder structure and append the file names to a `StringBuilder` object.
The following example takes the `GetFilesInFolderAsync` method from the previous example and adds the ability to retrieve the file size and date modified for each file. The example uses the [BasicProperties](/uwp/api/windows.storage.fileproperties.basicproperties) API to retrieve the file size and date modified for each file, formats the file size, and appends the size and date modified to the `StringBuilder` object after each file and folder name.
0 commit comments