Directory का उपयोग
Directory class folders से deal करती है: उन्हें बनाना, यह जांचना कि वे मौजूद हैं, और अंदर क्या है उसे list करना। यह आपकी filing cabinets के लिए एक caretaker जैसा है।
In this page:
Syntax
Directory.CreateDirectory(path);
Directory.Exists(path);
string[] files = Directory.GetFiles(path);
Directory.Delete(path);
Directory
Directory class folders बनाने, जांचने, list करने, और हटाने के लिए static methods प्रदान करती है, जैसे Directory.CreateDirectory और Directory.Exists।
Directory.GetFiles किसी folder के अंदर की files को paths के एक array के रूप में return करता है। यह file-level File class का folder-level counterpart है।
Note:
अगर directory पहले से मौजूद हो तो
Directory.CreateDirectory कुछ नहीं करता (और throw नहीं करता)।उदाहरण: Directory
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string folder = "reports";
Directory.CreateDirectory(folder);
Console.WriteLine(Directory.Exists(folder));
File.WriteAllText(Path.Combine(folder, "a.txt"), "data");
string[] files = Directory.GetFiles(folder);
foreach (string f in files)
{
Console.WriteLine(f);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- किसी ऐसे folder पर
Directory.Delete(path)call करना जिसमें अब भी files हों, जो एकIOExceptionफेंकता है; इसकी contents भी हटाने के लिए दूसरे argument के रूप मेंtrueपास करें। - किसी ऐसे folder पर
Directory.GetFilescall करना जो मौजूद नहीं, जो एकDirectoryNotFoundExceptionफेंकता है; पहलेDirectory.Existsजांचें। - यह मान लेना कि
"reports"जैसा एक relative path source file के पास है, जबकि यह program की current working directory के सापेक्ष है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: