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);
}
}
}
Do'stlaringiz bilan baham: |