2011-06-02 35 views
5

नोटपैड:नोटपैड का मान प्राप्त करें और इसे C# स्ट्रिंग के अंदर रखें?

Hello world! 

मैं इसे कैसे सी # में डाल दिया और यह स्ट्रिंग में बदल देंगे ..?

अब तक, मुझे नोटपैड का मार्ग मिल रहा है।

string notepad = @"c:\oasis\B1.text"; //this must be Hello world 

कृपया मुझे सलाह .. मैं इस पर परिचित नहीं हूँ .. tnx StreamReader के

+2

आप पूछ रहे हैं कि कैसे एक फ़ाइल नोटपैड में बनाया पढ़ें करने के लिए? – n8wrl

उत्तर

7

दिखाया फ़ाइल को पढ़ने आप का उपयोग करके पाठ पढ़ सकते हैं File.ReadAllText() विधि:

public static void Main() 
    { 
     string path = @"c:\oasis\B1.txt"; 

     try { 

      // Open the file to read from. 
      string readText = System.IO.File.ReadAllText(path); 
      Console.WriteLine(readText); 

     } 
     catch (System.IO.FileNotFoundException fnfe) { 
      // Handle file not found. 
     } 

    } 
+0

यह सही नहीं है, आपके पास फ़ाइल में आपके कॉल के बीच दौड़ की स्थिति है। एक्सलिस्ट और वास्तव में फ़ाइल को पढ़ना। अगर उस अवधि में फ़ाइल हटा दी जाती है, तो आपका समाधान क्रैश हो जाएगा। –

+0

@ ग्रेग डी, यह थोड़ा सा नाटकीय है, आपको नहीं लगता? क्या इस सवाल के बारे में कुछ भी है जो आपको लगता है कि कोड को उस बुलेट प्रूफ की आवश्यकता है? –

+0

@ ग्रेग डी, दूसरे विचार पर, मैंने [यहां आपका जवाब] पर ठोकर खाई [http://stackoverflow.com/questions/4509415/check-whether-a-folder-exists-in-a-path-in-c/ 4509517 # 4509517) और इस तरह बहुत कुछ। धन्यवाद! मैंने अपना जवाब अपडेट कर लिया है। (मैं अभी भी डाउनवोट से असहमत हूं :) –

5

मेकअप का उपयोग करें और नीचे के रूप में

string notepad = @"c:\oasis\B1.text"; 
StringBuilder sb = new StringBuilder(); 
using (StreamReader sr = new StreamReader(notepad)) 
      { 
       while (sr.Peek() >= 0) 
       { 
        sb.Append(sr.ReadLine()); 
       } 
      } 

string s = sb.ToString(); 
6

आप फ़ाइल की सामग्री, जैसे पढ़ने की जरूरत है:

using (var reader = new StreamReader(new FileStream(path, FileMode.Open, FileAccess.Read)) 
{ 
    return reader.ReadToEnd(); 
} 

या, जैसा कि बस के रूप में संभव:

return File.ReadAllText(path); 
3

Reading From a Text File (Visual C#), जब StreamReader बुलाया जा रहा है कि इस उदाहरण में @ नहीं किया जाता है, लेकिन जब आप लिखते हैं विजुअल स्टूडियो में कोड यह प्रत्येक \

के लिए नीचे दी गई त्रुटि देगा

न पहचाना गया एस्केप अनुक्रम

इस त्रुटि आप अपने पथ स्ट्रिंग की शुरुआत में है कि @" से पहले लिख सकते हैं से बचने के लिए। मैंने शोल का भी उल्लेख किया है कि अगर हम का उपयोग करते हैं तो भी हम यह त्रुटि नहीं देते हैं, भले ही हम @ नहीं लिखते हैं।

// Read the file as one string. 
System.IO.StreamReader myFile = new System.IO.StreamReader(@"c:\oasis\B1.text"); 
string myString = myFile.ReadToEnd(); 

myFile.Close(); 

// Display the file contents. 
Console.WriteLine(myString); 
// Suspend the screen. 
Console.ReadLine(); 
3

जांच इस उदाहरण:

// Read the file as one string. 
System.IO.StreamReader myFile = 
    new System.IO.StreamReader("c:\\test.txt"); 
string myString = myFile.ReadToEnd(); 

myFile.Close(); 

// Display the file contents. 
Console.WriteLine(myString); 
// Suspend the screen. 
Console.ReadLine();