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
"monitoring file system events", "FileSystemWatcher class, examples", "examples [C++], monitoring file system changes", "events [C++], monitoring", "file system events [C++]",
11
+
"files [C++], binary", "binary files, reading in C++",
12
+
"reading text files", "text files, reading",
13
+
"files [C++], retrieving information about", "FileInfo class",
14
+
"binary files, writing in C++", "files [C++], binary",
15
+
"files [C++], text", "text files, writing in C++"]
9
16
ms.assetid: 3296fd59-a83a-40d4-bd4a-6096cc13101b
10
17
author: "mikeblome"
11
18
ms.author: "mblome"
12
19
ms.workload: ["cplusplus", "dotnet"]
13
20
---
21
+
14
22
# File Handling and I/O (C++/CLI)
15
23
Demonstrates various file operations using the .NET Framework.
16
24
17
25
The following topics demonstrate the use of classes defined in the <xref:System.IO> namespace to perform various file operations.
18
26
19
-
## In This Section
27
+
## <aname="enumerate"></a> Enumerate Files in a Directory
28
+
The following code example demonstrates how to retrieve a list of the files in a directory. Additionally, the subdirectories are enumerated. The following code example uses the <xref:System.IO.Directory.GetFiles%2A><xref:System.IO.Directory.GetFiles%2A> and <xref:System.IO.Directory.GetDirectories%2A> methods to display the contents of the C:\Windows directory.
20
29
21
-
-[How to: Enumerate Files in a Directory (C++/CLI)](../dotnet/how-to-enumerate-files-in-a-directory-cpp-cli.md)
30
+
### Example
22
31
23
-
-[How to: Monitor File System Changes (C++/CLI)](../dotnet/how-to-monitor-file-system-changes-cpp-cli.md)
32
+
```cpp
33
+
// enum_files.cpp
34
+
// compile with: /clr
35
+
usingnamespaceSystem;
36
+
usingnamespaceSystem::IO;
24
37
25
-
-[How to: Read a Binary File (C++/CLI)](../dotnet/how-to-read-a-binary-file-cpp-cli.md)
38
+
int main()
39
+
{
40
+
String^ folder = "C:\\";
41
+
array<String^>^ dir = Directory::GetDirectories( folder );
-[How to: Retrieve File Information (C++/CLI)](../dotnet/how-to-retrieve-file-information-cpp-cli.md)
51
+
return 0;
52
+
}
53
+
```
54
+
55
+
## <a name="monitor"></a> Monitor File System Changes
56
+
The following code example uses <xref:System.IO.FileSystemWatcher> to register for events corresponding to files being created, changed, deleted, or renamed. Instead of periodically polling a directory for changes to files, you can use the <xref:System.IO.FileSystemWatcher> class to fire events when a change is detected.
30
57
31
-
-[How to: Write a Binary File (C++/CLI)](../dotnet/how-to-write-a-binary-file-cpp-cli.md)
58
+
### Example
32
59
33
-
-[How to: Write a Text File (C++/CLI)](../dotnet/how-to-write-a-text-file-cpp-cli.md)
60
+
```cpp
61
+
// monitor_fs.cpp
62
+
// compile with: /clr
63
+
#using <system.dll>
34
64
35
-
36
-
## See Also
37
-
[.NET Programming with C++/CLI (Visual C++)](../dotnet/dotnet-programming-with-cpp-cli-visual-cpp.md)
Console::WriteLine("Press Enter to quit the sample.");
116
+
Console::ReadLine( );
117
+
}
118
+
```
119
+
120
+
## <aname="read_binary"></a> Read a Binary File
121
+
The following code example shows how to read binary data from a file, by using two classes from the <xref:System.IO?displayProperty=fullName> namespace: <xref:System.IO.FileStream> and <xref:System.IO.BinaryReader>. <xref:System.IO.FileStream> represents the actual file. <xref:System.IO.BinaryReader> provides an interface to the stream that allows binary access.
122
+
123
+
The code example reads a file that's named data.bin and contains integers in binary format. For information about this kind of file, see [How to: Write a Binary File (C++/CLI)](../dotnet/how-to-write-a-binary-file-cpp-cli.md).
while (br->BaseStream->Position < br->BaseStream->Length)
144
+
Console::WriteLine(br->ReadInt32().ToString());
145
+
146
+
fs->Close( );
147
+
}
148
+
catch (Exception^ e)
149
+
{
150
+
if (dynamic_cast<FileNotFoundException^>(e))
151
+
Console::WriteLine("File '{0}' not found", fileName);
152
+
else
153
+
Console::WriteLine("Exception: ({0})", e);
154
+
return -1;
155
+
}
156
+
return 0;
157
+
}
158
+
```
159
+
## <a name="read_text"></a> Read a Text File
160
+
The following code example demonstrates how to open and read a text file one line at a time, by using the <xref:System.IO.StreamReader> class that's defined in the <xref:System.IO?displayProperty=fullName> namespace. An instance of this class is used to open a text file and then the <xref:System.IO.StreamReader.ReadLine%2A?displayProperty=fullName> method is used to retrieve each line.
161
+
162
+
This code example reads a file that's named textfile.txt and contains text. For information about this kind of file, see [How to: Write a Text File (C++/CLI)](../dotnet/how-to-write-a-text-file-cpp-cli.md).
163
+
164
+
### Example
165
+
166
+
```cpp
167
+
// text_read.cpp
168
+
// compile with: /clr
169
+
#using<system.dll>
170
+
using namespace System;
171
+
using namespace System::IO;
172
+
173
+
int main()
174
+
{
175
+
String^ fileName = "textfile.txt";
176
+
try
177
+
{
178
+
Console::WriteLine("trying to open file {0}...", fileName);
179
+
StreamReader^ din = File::OpenText(fileName);
180
+
181
+
String^ str;
182
+
int count = 0;
183
+
while ((str = din->ReadLine()) != nullptr)
184
+
{
185
+
count++;
186
+
Console::WriteLine("line {0}: {1}", count, str );
187
+
}
188
+
}
189
+
catch (Exception^ e)
190
+
{
191
+
if (dynamic_cast<FileNotFoundException^>(e))
192
+
Console::WriteLine("file '{0}' not found", fileName);
## <aname="retrieve"></a> Retrieve File Information
202
+
The following code example demonstrates the <xref:System.IO.FileInfo> class. When you have the name of a file, you can use this class to retrieve information about the file such as the file size, directory, full name, and date and time of creation and of the last modification.
203
+
204
+
This code retrieves file information for Notepad.exe.
## <a name="write_binary"></a> Write a Binary File
242
+
The following code example demonstrates writing binary data to a file. Two classes from the <xref:System.IO> namespace are used: <xref:System.IO.FileStream> and <xref:System.IO.BinaryWriter>. <xref:System.IO.FileStream> represents the actual file, while <xref:System.IO.BinaryWriter> provides an interface to the stream that allows binary access.
243
+
244
+
The following code example writes a file containing integers in binary format. This file can be read with the code in [How to: Read a Binary File (C++/CLI)](../dotnet/how-to-read-a-binary-file-cpp-cli.md).
The following code example demonstrates how to create a text file and write text to it using the <xref:System.IO.StreamWriter> class, which is defined in the <xref:System.IO> namespace. The <xref:System.IO.StreamWriter> constructor takes the name of the file to be created. If the file exists, it is overwritten (unless you pass True as the second <xref:System.IO.StringWriter> constructor argument).
285
+
286
+
The file is then filed using the <xref:System.IO.StreamWriter.Write%2A> and <xref:System.IO.TextWriter.WriteLine%2A> functions.
287
+
288
+
### Example
289
+
290
+
```cpp
291
+
// text_write.cpp
292
+
// compile with: /clr
293
+
usingnamespaceSystem;
294
+
usingnamespaceSystem::IO;
295
+
296
+
int main()
297
+
{
298
+
String^ fileName = "textfile.txt";
299
+
300
+
StreamWriter^ sw = gcnew StreamWriter(fileName);
301
+
sw->WriteLine("A text file is born!");
302
+
sw->Write("You can use WriteLine");
303
+
sw->WriteLine("...or just Write");
304
+
sw->WriteLine("and do {0} output too.", "formatted");
305
+
sw->WriteLine("You can also send non-text objects:");
306
+
sw->WriteLine(DateTime::Now);
307
+
sw->Close();
308
+
Console::WriteLine("a new file ('{0}') has been written", fileName);
309
+
310
+
return 0;
311
+
}
312
+
```
313
+
314
+
## See Also
315
+
[.NET Programming with C++/CLI (Visual C++)](../dotnet/dotnet-programming-with-cpp-cli-visual-cpp.md)
316
+
317
+
[File and Stream I-O](http://msdn.microsoft.com/Library/4f4a33a9-66b7-4cd7-a285-4ad3e4276cd2)
0 commit comments