Harry Potter and the Sorcerer's Stone


Fayllarni C# yordamida katalogga oling


Download 192.49 Kb.
bet41/62
Sana17.06.2023
Hajmi192.49 Kb.
#1535967
1   ...   37   38   39   40   41   42   43   44   ...   62
Bog'liq
Algaritim amaliy-2

26. Fayllarni C# yordamida katalogga oling.
Javob:
Fayllarni katalogga olish uchun `File.Copy()` metodi ishlatiladi. Quyidagi kod orqali `
C:\Users\OpenAI\Desktop\test.txt` faylini `C:\Users\OpenAI\Desktop\Yangi_katalog` katalogiga nusxalash mumkin:
csharp
string eski_fayl_nomi = @"C:\Users\OpenAI\Desktop\test.txt";
string yangi_katalog_nomi = @"C:\Users\OpenAI\Desktop\Yangi_katalog";
string yangi_fayl_nomi = Path.Combine(yangi_katalog_nomi, Path.GetFileName(eski_fayl_nomi));
File.Copy(eski_fayl_nomi, yangi_fayl_nomi);
27. C# yordamida katalogdagi kataloglarni oling.
Javob:
C# yordamida katalogdagi kataloglarni olish uchun siz Directory.GetDirectories() usulidan foydalanishingiz mumkin.
using System.IO;
string directoryPath = "C:\\example"; // replace with the path to your directory
string[] directories = Directory.GetDirectories(directoryPath);
Bu sizga belgilangan katalogdagi barcha kataloglarga yo'llarni ifodalovchi qatorlar qatorini beradi.
Siz ixtiyoriy ravishda natijalarni filtrlash uchun qidiruv namunasini ham belgilashingiz mumkin. Masalan, faqat “a” harfi bilan boshlanadigan kataloglarni olish uchun siz:
dan foydalanishingiz mumkin
string[] directories = Directory.GetDirectories(directoryPath, "a*");
Bu sizga belgilangan katalogdagi "a" harfi bilan boshlanadigan barcha kataloglarga yo'llarni ifodalovchi qatorlar qatorini beradi.
28. C# yordamida fayl tizimining ildiz kataloglarini oling.
Javob:
Siz fayl tizimining ildiz kataloglarini C# da DriveInfo sinfidan foydalanib olishingiz mumkin.
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
Console.WriteLine(drive.RootDirectory);
}
Ushbu kod mashinadagi barcha fayl tizimlarining ildiz kataloglarini chop etadi. Umid qilamanki, bu yordam beradi!
29. C# yordamida fayl yoki katalogning asosiy katalogini oling.
Javob:
C# da fayl yoki katalogning asosiy katalogini olish uchun Directory.GetParent() usulidan foydalanishingiz mumkin.
string filePath = @"C:\Users\abdumominov_live\Desktop\example.txt";
string parentDirPath = Directory.GetParent(filePath)?.FullName;
Console.WriteLine(parentDirPath);
Ushbu kod example.txt faylining asosiy katalog yo'lini chop etadi. GetParent() dan keyingi ? null-shartli operator deb ataladi, bu esa ko'rsatilgan fayl yo'li uchun ota-katalog bo'lmagan taqdirda null qiymat qaytarilishini ta'minlaydi.< /p>

Download 192.49 Kb.

Do'stlaringiz bilan baham:
1   ...   37   38   39   40   41   42   43   44   ...   62




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