Directory sinfidan foydalanish Directory sinfining metodlari va ular bilan ishlash 6
Download 0.55 Mb.
|
Dasturlash asoslari fanidan C# dasturlash tilining Directory,
- Bu sahifa navigatsiya:
- II BOB. C DASTURLASH TILINING DIRECTORYINFO SINFI VA U BILAN ISHLASH
Foydalanish misoli:
using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { try { string[] dirs = Directory.GetDirectories(@"C:\", "p*", SearchOption.TopDirectoryOnly); Console.WriteLine("p bilan boshlanuvchi kataloglarning soni {0}ta.", dirs.Length); foreach (string dir in dirs) { Console.WriteLine(dir); } } catch (Exception e) { Console.WriteLine("Jarayon muvaffaqiyatsiz tugadi: {0}", e.ToString()); } Console.ReadKey(true); } } } Natijasi: GetDirectoryRoot(String) – ko'rsatilgan katalogning ildiz katalogini qaytaradi. Foydalanish misoli: using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { string root = @"Dasturlash"; Console.WriteLine(Directory.GetDirectoryRoot(root)); Console.ReadKey(true); } } } Natijasi: GetFiles(String) – belgilangan katalogdagi fayllarning ro’yxatini (shu jumladan yo’llarini) qaytaradi. Foydalanish misoli: using System; using System.IO; namespace ListFiles { class Program { static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] myFiles = Directory.GetFiles(docPath); Console.WriteLine("Fayllar:"); foreach (var myFile in myFiles) { Console.WriteLine(myFile); } Console.ReadKey(); } } } Natijasi: GetFiles(String, String) – belgilangan katalogdagi qidiruv namunasiga mos keladigan fayllarning ro’yxatini (shu jumladan yo’llarini) qaytaradi. Foydalanish misoli: using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] myFiles = Directory.GetFiles(docPath, "b*"); Console.WriteLine("Fayllar:"); foreach (var myFile in myFiles) { Console.WriteLine(myFile); } Console.ReadKey(); } } } Natijasi: GetFiles(String, String, SearchOption) – pastki kataloglarni qidirish yoki yo’qligini aniqlash qiymati yordamida belgilangan katalogdagi qidiruv namunasiga mos keladigan fayllarning ro’yxatini (shu jumladan yo’llarini) qaytaradi. Foydalanish misoli: using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] myFiles = Directory.GetFiles(docPath, "d*", SearchOption.TopDirectoryOnly); Console.WriteLine("Fayllar:"); foreach (var myFile in myFiles) { Console.WriteLine(myFile); } Console.ReadKey(); } } } Natijasi: GetFileSystemEntries(String) – belgilangan yo’lda barcha fayllar va pastki kataloglarning nomlarini qaytaradi. Foydalanish misoli: using System; using System.IO; namespace ListEntries { class Program { static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] entries = Directory.GetFileSystemEntries(docPath); Console.WriteLine("Ro'yxatlar:"); foreach (var entry in entries) { Console.WriteLine(entry); } Console.ReadKey(); } } } Natijasi: GetFileSystemEntries(String,String) – belgilangan yo’lda qidirish namunasiga mos keladigan fayl nomlari va katalog nomlarini qaytaradi. Foydalanish misoli: using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] entries = Directory.GetFileSystemEntries(docPath, "м*"); Console.WriteLine("Ro'yxatlar:"); foreach (var entry in entries) { Console.WriteLine(entry); } Console.ReadKey(); } } } Natijasi: GetFileSystemEntries(String, String, SearchOption) – belgilangan yo’lda qidirish namunasiga mos keladigan barcha fayl nomlari va katalog nomlarini va ixtiyoriy ravishda pastki kataloglarni qaytaradi. Foydalanish misoli: using System; using System.IO; namespace Dir { class Program { public static void Main(string[] args) { var docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string[] entries = Directory.GetFileSystemEntries(docPath, "м*", SearchOption.TopDirectoryOnly); Console.WriteLine("Ro'yxatlar:"); foreach (var entry in entries) { Console.WriteLine(entry); } Console.ReadKey(); } } } Natijasi: GetLogicalDrives() – ushbu kompyuterdagi mantiqiy drayverlarning nomlarini “ Foydalanish misoli: using System; using System.IO; namespace dir { class Program { public static void Main(string[] args) { string[] drives = Directory.GetLogicalDrives(); Console.WriteLine("Kompyuterdagi disklar ro'yxati:"); foreach (string drive in drives) { Console.WriteLine(drive); } Console.ReadKey(true); } } } Natijasi: II BOB. C# DASTURLASH TILINING DIRECTORYINFO SINFI VA U BILAN ISHLASH2.1 DirectoryInfo sinfidan foydalanishDirectoryInfo sinfi kataloglar bilan ishlashning namunaviy usullarini taqdim etadi. DirectoryInfo sinfi katalog haqida ma'lumot beradi, uni DirectoryInfo ob'ektini yaratishda konstruktor orqali joylash mumkin. Fayl tizimidagi katalog bilan bog'langan DirectoryInfo ob'ekti qo’lga kiritilgach, biz har qanday ma'lumotni, shu jumladan tarkibidagi fayllarni ham olishimiz mumkin. FileInfo sinfi singari, DirectoryInfo sinfi ham kirish parametr sifatida katalogga to'liq yo'lni yoki nisbiy yo'lni oladigan yagona konstruktorga ega: DirectoryInfo d1 = new DirectoryInfo(@"c:\temp"); // Katalog ob'ekti yaratish DirectoryInfo sinfining ba'zi muhim xususiyatlari, masalan, CreationTime, Exists, FullName, LastAccessTime, LastWriteTime, Parent, Name va Root, bular nomlaridan kelib chiqib ishlaydi. Masalan, CreationTime katalogni yaratilish vaqtini ko'rsatadi. FullName esa katalogning to'liq yetarli yo'li, Name esa shunchaki nisbiy papkaning nomi bo'lib, yo'l yo'q (masalan, to'liq nomi c:\Mening loyiham\test bo'lgan katalogning nomi test bo'ladi). Ma’lumot turi System.IO.FileSystemInfo dan meros bo'lib o'tgan, shuning uchun u ona sinfning juda ko'p yuklangan metodlari, shuningdek kataloglar bilan ishlashda o'nlab foydali xususiyatlarga ega: Attributesdan foydalanib, masalan, katalogning atributlarini o'qish yoki so'rash mumkin; Extension – katalog ichidagi fayl kengaytmalari haqida ma'lumot beradi; Exists – katalog mavjudligini tekshiradi; Name, Parent, Root – katalog nomini, ona yoki ildiz katalogiga yo'lni qaytaradi. FullName – katalogning to’liq yo’lini qaytaradi. DirectoryInfo ma’lumot turi (sinfi) bo'lganligi sababli, uning a'zolaridan foydalanishdan oldin, ob'ektni konstruktor yordamida yaratish kerak. Konstruktor parametri sifatida katalog nomi va yo'lini satrda uzatish kerak. Katalogga yaratilgan DirectoryInfo nusxasi joriy ishchi katalog sifatida tayinlanadi. DirectoryInfo firstDir = new DirectoryInfo(@"C:\Temp"); DirectoryInfo secondDir = new DirectoryInfo("."); Konstruktor kataloglarni yaratmaydi, faqat ularni bog'laydi. Agar konstruktorga boradigan yo'l bilan birga yuborilgan katalog mavjud bo'lmasa, uni ishlatishdan oldin Create() metodidan foydalanib yaratish kerak, aks holda undan foydalanish istisnolarni keltirib chiqaradi. DirectoryInfo firstDir = new DirectoryInfo(@"C:\Temp"); firstDir.Create(); Ob'ektni try/catch blokida ishlatishdan oldin Exist xususiyatidan foydalanish afzalroq: Download 0.55 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling