Write a program in C# Sharp to create a file with text and read the file
- برمجة سي شارب
- برمجة
- 2021-06-01
- ahmadghneem
الأجوبة
using System;
using System.IO;
using System.Text;
class filexercise3
{
public static void Main()
{
string fileName = @"mytest.txt";
try
{
// Delete the file if it exists.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Console.Write("\n\n Create a file with text and read the file :\n");
Console.Write("-------------------------------------------------\n");
// Create the file.
using (StreamWriter fileStr = File.CreateText(fileName))
{
fileStr.WriteLine(" Hello and Welcome");
fileStr.WriteLine(" It is the first content");
fileStr.WriteLine(" of the text file mytest.txt");
}
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
Console.WriteLine(" Here is the content of the file mytest.txt : ");
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
Console.WriteLine("");
}
}
catch (Exception MyExcep)
{
Console.WriteLine(MyExcep.ToString());
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال