C# में एक नई फ़ाइल बनाने के लिए, FileStream ऑब्जेक्ट का उपयोग करें।
निम्नलिखित वाक्य रचना है -
FileStream <object_name> = new FileStream( <file_name>, <FileMode Enumerator>, <FileAccess Enumerator>, <FileShare Enumerator>);
आइए हम एक फ़ाइल “test.dat” के लिए एक उदाहरण देखें, जिसे फ़ाइल ऑब्जेक्ट का उपयोग करके बनाया/खोला गया है -
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,FileAccess.ReadWrite); निम्नलिखित एक उदाहरण है -
उदाहरण
using System;
using System.IO;
namespace FileIOApplication {
class Program {
static void Main(string[] args) {
FileStream F = new FileStream("test.dat", FileMode.OpenOrCreate,
FileAccess.ReadWrite);
for (int i = 1; i <= 20; i++) {
F.WriteByte((byte)i);
}
F.Position = 0;
for (int i = 0; i <= 20; i++) {
Console.Write(F.ReadByte() + " ");
}
F.Close();
Console.ReadKey();
}
}
}