Path का उपयोग
Path class आपको file addresses को सही तरीके से बनाने और अलग करने में मदद करती है, जैसे एक mapmaker जो directions ठीक से लिखता है। यह file को छुए बिना address के text के साथ काम करती है।
In this page:
Syntax
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
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));
}
}
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. #}
आम गलतियां
+से paths को concatenate करना और गलत separators पाना;Path.Combineउपयोग करें।- यह मान लेना कि
Path.Combineयह validate करता है कि path मौजूद है। Path.Combineको एक absolute second path पास करना, जो पहले हिस्से को discard कर देता है।
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: