“Dasturlash asoslari” fanidan “C# dasturlash tilining Directory, DirectoryInfo va DriveInfo sinflari hamda ular bilan ishlash”


Download 0.63 Mb.
bet4/6
Sana12.11.2020
Hajmi0.63 Mb.
#144473
1   2   3   4   5   6
Bog'liq
kurs ishi


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:



Download 0.63 Mb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6




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