Fizika matematika fakulteti



Download 0,54 Mb.
bet11/17
Sana11.01.2022
Hajmi0,54 Mb.
#347198
1   ...   7   8   9   10   11   12   13   14   ...   17
Bog'liq
Dasturlash asoslari fanidan C# dasturlash tilining Directory,

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 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[] 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 yo’llarini) qaytaradi.

GetDirectories(String, String, SearchOption) – belgilangan katalogdagi belgilangan qidiruv namunasiga mos keladigan pastki kataloglarning nomlarini (shu jumladan yo’llarini) 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 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 “:\” 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 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:



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 to’xtatildi: {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.



Download 0,54 Mb.

Do'stlaringiz bilan baham:
1   ...   7   8   9   10   11   12   13   14   ...   17




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish