← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 3 of 6

Directory का उपयोग

Directory class folders से deal करती है: उन्हें बनाना, यह जांचना कि वे मौजूद हैं, और अंदर क्या है उसे list करना। यह आपकी filing cabinets के लिए एक caretaker जैसा है।

In this page:

  1. Directory
Syntax
csharp
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

markup
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);
        }
    }
}
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. #}
आम गलतियां
  1. किसी ऐसे folder पर Directory.Delete(path) call करना जिसमें अब भी files हों, जो एक IOException फेंकता है; इसकी contents भी हटाने के लिए दूसरे argument के रूप में true पास करें।
  2. किसी ऐसे folder पर Directory.GetFiles call करना जो मौजूद नहीं, जो एक DirectoryNotFoundException फेंकता है; पहले Directory.Exists जांचें।
  3. यह मान लेना कि "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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.