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

File.ReadAllText क्या है

File.ReadAllText एक file खोलता है, अंदर का सारा text पढ़ता है, और इसे एक ही बार में वापस दे देता है, जैसे एक ही झटके में पूरी चिट्ठी पढ़ना। यह किसी छोटी file पढ़ने का सबसे तेज़ तरीका है।

In this page:

  1. File.ReadAllText
Syntax
csharp
string content = File.ReadAllText(path);
File.WriteAllText(path, content);

File.ReadAllText

File.ReadAllText एक पूरी file को एक call में एक single string में पढ़ता है, जो छोटी-से-मध्यम text files load करने का सबसे सरल तरीका है। File.WriteAllText लिखने के लिए इसका counterpart है।

दोनों System.IO.File class पर static methods हैं और file को अपने आप खोलते, उपयोग करते, और बंद करते हैं।

Note: बहुत बड़ी files के लिए, StreamReader को प्राथमिकता दें ताकि आप file को एक साथ memory में load करने के बजाय line by line process कर सकें।

उदाहरण: File.ReadAllText

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string path = "greeting.txt";
        File.WriteAllText(path, "Hello from File.WriteAllText!");

        string content = File.ReadAllText(path);
        Console.WriteLine(content);
    }
}
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. File.ReadAllText से एक विशाल file पढ़ना, जो इसे पूरी memory में load कर देता है।
  2. गलत relative path उपयोग करना, जिससे FileNotFoundException फेंका जाता है।
  3. IOException या UnauthorizedAccessException handle करना भूल जाना।
🔒

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.