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

Path का उपयोग

Path class आपको file addresses को सही तरीके से बनाने और अलग करने में मदद करती है, जैसे एक mapmaker जो directions ठीक से लिखता है। यह file को छुए बिना address के text के साथ काम करती है।

In this page:

  1. Path
Syntax
csharp
string full = Path.Combine(segment1, segment2, "file.txt");
Path.GetFileName(full);
Path.GetExtension(full);
Path.GetDirectoryName(full);

Path

Path class filesystem को छुए बिना file path strings में हेरफेर करती है — segments को combine करना, file names निकालना, और extensions पाना।

Path.Combine current operating system के लिए एक सही path बनाता है, manual slash handling से बचते हुए। string concatenation के बजाय Path methods उपयोग करना cross-platform path bugs से बचाता है।

Note: manually / या \\ से folder names को concatenate करने के बजाय हमेशा Path.Combine उपयोग करें।

उदाहरण: Path

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string fullPath = Path.Combine("data", "reports", "summary.txt");

        Console.WriteLine(fullPath);
        Console.WriteLine(Path.GetFileName(fullPath));
        Console.WriteLine(Path.GetExtension(fullPath));
        Console.WriteLine(Path.GetFileNameWithoutExtension(fullPath));
    }
}
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. + से paths को concatenate करना और गलत separators पाना; Path.Combine उपयोग करें।
  2. यह मान लेना कि Path.Combine यह validate करता है कि path मौजूद है।
  3. Path.Combine को एक absolute second path पास करना, जो पहले हिस्से को discard कर देता है।
🔒

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.