Foydalanish misoli:
using System;
using System.IO;
namespace Dir
{
class Program
{
public static void Main(string[] args)
{
DirectoryInfo di = new DirectoryInfo(@"C:\TempDir");
if (di.Exists == false)
di.Create();
Console.WriteLine("Katalog muvafaqqiyatli yaratildi.");
DirectoryInfo dis = di.CreateSubdirectory("SubDir");
Console.WriteLine("Pastki katalog muvafaqqiyatli yaratildi.");
dis.Delete(true);
Console.WriteLine("Pastki katalog muvaffaqiyatli o'chirildi.");
di.Delete(true);
Console.WriteLine("Katalog muvaffaqiyatli o'chirildi.");
Console.ReadKey();
}
}
}
Natijasi:
Delete() – agar u bo'sh bo'lsa, bu DirectoryInfo ni yo'q qiladi.
Foydalanish misoli:
using System;
using System.IO;
class Test
{
public static void Main()
{
DirectoryInfo di1 = new DirectoryInfo(@"C:\MyDir");
try
{
di1.Create();
di1.CreateSubdirectory("temp");
Console.WriteLine("Men {0}ni o'chirishga harakat qilyapman.", di1.Name);
di1.Delete();
Console.WriteLine("Yo'q qilish operatsiyasi tasodifan muvafaqqiyatli yuz berdi.");
}
catch (Exception)
{
Console.WriteLine("Yo'q qilish operatsiyasi kutilganidek muvafaqqiyatsiz yuz berdi.");
}
finally {}
Console.ReadKey();
}
}
Natijasi:
Delete(Boolean) – pastki kataloglar va fayllarni yo'q qilish kerakligini aniqlab, DirectoryInfo ning ushbu nusxasini yo'q qiladi.
Foydalanish misoli:
DirectoryInfo di = new DirectoryInfo(@"C:\TempDir");
if (di.Exists == false)
di.Create();
DirectoryInfo dis = di.CreateSubdirectory("SubDir");
Console.WriteLine("Katalog va pastki katalog muvafaqqiyatli yaratildi.");
//True shuni ko'rsatadiki, agar ushbu katalogda pastki kataloglar
// yoki fayllar bo'lsa, ular ham o'chiriladi.
di.Delete(true);
Console.WriteLine("Katalog va pastki katalog muvafaqqiyatli o'chirildi.");
Console.ReadKey();
Natijasi:
GetDirectories() – belgilangan katalogdagi pastki kataloglarning nomlarini (shu jumladan yo’llarini) qaytaradi.
Foydalanish misoli:
using System;
using System.IO;
public class GetDirectoriesTest
{
public static void Main()
{
DirectoryInfo di = new DirectoryInfo("C:\\");
DirectoryInfo[] diArr = di.GetDirectories();
Console.Write(" 'C:\\' diskdagi kataloglar:");
Console.WriteLine();
foreach (DirectoryInfo dri in diArr)
Console.WriteLine("\t" +dri.Name);
Console.ReadKey();
}
}
Natijasi:
GetDirectories(String) – belgilangan katalogdagi qidiruv uslubiga mos keladigan pastki kataloglarning nomlarini (shu jumladan yo’llarini) qaytaradi.
GetDirectories(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;
public class GetDirectoriesTest
{
public static void Main()
{
DirectoryInfo di = new DirectoryInfo("C:\\");
DirectoryInfo[] diArr = di.GetDirectories("p*"); //("p*", SearchOption.TopDirectoryOnly)
Console.Write(" 'C:\\' diskdagi 'p' harfi bilan boshlanuvchi kataloglar:");
Console.WriteLine();
foreach (DirectoryInfo dri in diArr)
Console.WriteLine("\t" +dri.Name);
Console.ReadKey();
}
}
Natijasi:
EnumerateDirectories() – joriy katalogdagi ma'lumotlarni sanaydigan to'plamni qaytaradi.
Foydalanish misoli:
using System;
using System.IO;
using System.Linq;
namespace EnumDir
{
class Program
{
static void Main(string[] args)
{
string docPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
DirectoryInfo dirPrograms = new DirectoryInfo(docPath);
DateTime StartOf2019 = new DateTime(2019, 12, 04);
var dirs = from dir in dirPrograms.EnumerateDirectories()
where dir.CreationTimeUtc > StartOf2019
select new
{
ProgDir = dir,
};
Console.WriteLine("(2019, 12, 04) sanadan boshlab yaratilgan kataloglar ro'yxati:"+"\n");
foreach (var di in dirs)
{
Console.WriteLine(di.ProgDir.Name);
}
Console.ReadKey();
}
}
}
Natijasi:
EnumerateDirectories(String) – belgilangan yo’lda qidirish namunasiga mos keladigan katalogning to’liq nomlarini sanaydigan to’plamni qaytaradi.
Foydalanish misoli:
Public System.Collections.Generic.IEnumerable
EnumerateDirectories (string searchPattern);
EnumerateDirectories(String, SearchOption) – belgilangan yo’lda qidirish namunasiga mos keladigan katalogning to’liq nomlarini sanaydigan to’plamini va ixtiyoriy ravishda qidirilgan pastki kataloglarni qaytaradi.
Foydalanish misoli:
public System.Collections.Generic.IEnumerable
EnumerateDirectories (string searchPattern, SearchOption searchOption);
MoveTo() – DirectoryInfo nusxasini va uning tarkibini yangi yo'lga ko'chiradi.
Foydalanish misoli:
using System;
using System.IO;
public class MoveToTest
{
public static void Main()
{
string oldPath = @"C:\SomeFolder";
string newPath = @"C:\SomeDir";
DirectoryInfo dirInfo = new DirectoryInfo(oldPath);
if (dirInfo.Exists && Directory.Exists(newPath) == false)
{
dirInfo.MoveTo(newPath);
Console.WriteLine("Katalog muvaffaqiyatli ko'chirildi.");
}
Console.ReadKey();
}
}
Natijasi:
Do'stlaringiz bilan baham: |