Harry Potter and the Sorcerer's Stone


Yangi katalog yaratish uchun C# dasturini yozing


Download 192.49 Kb.
bet51/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   47   48   49   50   51   52   53   54   ...   62
Bog'liq
Algaritim amaliy-2

63. Yangi katalog yaratish uchun C# dasturini yozing.
Javob:
using System.IO;
class Program
{
static void Main(string[] args)
{
string directoryPath = "new_directory";
try
{
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
Console.WriteLine("Yangi katalog yaratildi.");
}
else
{
Console.WriteLine("Katalog allaqachon mavjud.");
}
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message);
}
}
}
64. Faylni o'chirish uchun C# dasturini yozing.
Javob:
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "test.txt";
try
{
if (File.Exists(filePath))
{
File.Delete(filePath);
Console.WriteLine("Fayl o'chirildi.");
}
else
{
Console.WriteLine("Fayl topilmadi.");
}
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message);
}
}
}
65. Mavjud faylga matn qo'shish uchun C# dasturini yozing.
Javob:
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "test.txt";
try
{
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("Bu yangi qator.");
Console.WriteLine("Matn faylga qo'shildi.");
}
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message);
}
}
}
66. Fayl hajmini olish uchun C# dasturini yozing.
Javob:
using System.IO;
class Program
{
static void Main(string[] args)
{
string filePath = "test.txt";
try
{
FileInfo fileInfo = new FileInfo(filePath);
long fileSize = fileInfo.Length;
Console.WriteLine("Fayl hajmi: " + fileSize + " bayt.");
}
catch (Exception ex)
{
Console.WriteLine("Xatolik yuz berdi: " + ex.Message);
}
}
}

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   47   48   49   50   51   52   53   54   ...   62




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling