Foydalanish misoli:
using System;
using System.IO;
namespace Dir
{
class Program
{
public static void Main(string[] args)
{
string root = @"C:\Temp";
// katalogning oxirgi yozilish vaqtini belgilash va olish
Directory.SetLastWriteTime(root, DateTime.Now);
DateTime lastWriteTime =Directory.GetLastWriteTime(root);
Console.WriteLine("So'nggi yozish sanasi va vaqti:
"+lastWriteTime);
Console.ReadKey(true);
}
}
}
Natijasi:
GetCurrentDirectory() ilovaning joriy ishchi katalogini oladi.
SetCurrentDirectory(String) belgilangan katalogni joriy katalog sifatida o'rnatadi.
Foydalanish misoli:
using System;
using System.IO;
namespace Dir
{
class Program
{
public static void Main(string[] args)
{
string root = @"C:\Windows";
Directory.SetCurrentDirectory(root);
Console.WriteLine(Directory.GetCurrentDirectory());
Console.ReadKey(true);
}
}
}
Natijasi:
GetDirectories(String) belgilangan katalogdagi pastki kataloglarning nomlarini (shu jumladan yollarini) 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[] myDirs = Directory.GetDirectories(docPath);
Console.WriteLine("Kataloglar:");
foreach (var myDir in myDirs)
{
Console.WriteLine(myDir);
}
Console.ReadKey(true);
}
}
}
Natijasi:
GetDirectories(String, String) belgilangan katalogdagi qidiruv uslubiga mos keladigan pastki kataloglarning nomlarini (shu jumladan yollarini) qaytaradi.
GetDirectories(String, String, SearchOption) belgilangan katalogdagi belgilangan qidiruv namunasiga mos keladigan pastki kataloglarning nomlarini (shu jumladan yollarini) va ixtiyoriy ravishda pastki kataloglarni qaytaradi.
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 royxatini (shu jumladan yollarini) 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 royxatini (shu jumladan yollarini) 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 yoqligini aniqlash qiymati yordamida belgilangan katalogdagi qidiruv namunasiga mos keladigan fayllarning royxatini (shu jumladan yollarini) 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 yolda 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 yolda 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 yolda 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 :\ shaklida qaytaradi.
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 ISHLASH 2.1 DirectoryInfo sinfidan foydalanish
DirectoryInfo 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 qolga 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 toliq yolini qaytaradi.
DirectoryInfo malumot 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:
Foydalanish misoli:
using System;
using System.IO;
namespace dir
{
class Program
{
public static void Main(string[] args)
{
DirectoryInfo firstDir = new DirectoryInfo(@"C:\Temp");
try
{
if (firstDir.Exists)
{
Console.WriteLine("Bu yo'l allaqachon mavjud.");
return;
}
// Katalog yaratish
firstDir.Create();
Console.WriteLine("Katalog muvaffaqiyatli yaratildi.");
// Endi o'chirish
firstDir.Delete();
Console.WriteLine("Katalog muvaffaqiyatli o'chirildi.");
}
catch (Exception e)
{
Console.WriteLine("Jarayon toxtatildi: {0}", e.ToString());
}
Console.ReadKey(true);
}
}
}
Natijasi:
DirectoryInfo sinfining maqsadi Directory bilan bir xil. Biroq, DirectoryInfo bir xil ob'ekt (katalog) ko'p ishlatiladigan operatsiyalar uchun ishlatiladi. Qaytadan ro'yxatdan o'tkazmaslik uchun tekshirishni boshlashning har bir bosqichida ob'ekt havolasidan foydalanish qulayroqdir.
Do'stlaringiz bilan baham: |