File handling concept in C#:-
The file is a collection of records, It is used to store information permanently under the client machine.
C# provides IO namespace to perform the file handling task.
Operation on File:-
1) Write File:-
The file is a collection of records, It is used to store information permanently under the client machine.
C# provides IO namespace to perform the file handling task.
Operation on File:-
1) Write File:-
It is used to save the record on file
using System.IO;
class FileHandlingOperation
{
public static void Main()
{
File.WriteAllText("D://mydemo.txt", "Welcome in File Handling");
Console.ReadKey();
}
}
2) Read File
class FileHandlingOperation
{
public static void Main()
{
String s= File.ReadAllText("D://mydemo.txt");
Console.WriteLine(s);
Console.ReadKey();
}
}
3) Append File Operation:-
class FileHandlingOperation
{
public static void Main()
{
File.AppendAllText("D://mydemo.txt", "Welcome in File Handling");
Console.ReadKey();
}
}
Another Example of File Handling Operation:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Project2
{
class FileHandlingOperation
{
string data = "";
void CreateFile()
{
FileStream fs= File.Create("D://mydata1.txt");
fs.Close();
}
void WriteFile()
{
// Console.WriteLine("Enter data");
// data = Console.ReadLine();
File.WriteAllText("D://si.txt",data);
float p = 50000, r=2, t=3, si;
si = (p * r * t) / 100;
File.WriteAllText("D://si.txt", "Result is "+si);
}
void ReadFile()
{
String s = File.ReadAllText("D://mydata.txt");
Console.WriteLine(s);
}
void AppendFile()
{
Console.WriteLine("Enter data");
data = Console.ReadLine();
File.AppendAllText("D://mydata.txt",data);
}
static void Main()
{
FileHandlingOperation obj = new FileHandlingOperation();
// obj.CreateFile();
obj.WriteFile();
// obj.AppendFile();
obj.ReadFile();
Console.ReadKey();
}
}
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,............................................................
POST Answer of Questions and ASK to Doubt