هذه المقالة مترجمة يدويًا. حرك المؤشر فوق الجمل في المقالة لعرض النص الأصلي. المزيد من المعلومات.
الترجمة
الأصلي
لم يتم تصنيف هذا الموضوع بعد - تصنيف هذا الموضوع

كيفية القيام بما يلي: القراءة من ملف نصي (دليل البرمجة لـ #C)

Visual Studio 2010

يقوم هذا المثال بقراءة محتويات ملف نصي باستخدام الأساليب الثابتة ReadAllText و ReadAllLines في الفئة System.IO.File.

ملاحظة ملاحظة

تم إنشاء الملفات المستخدمة في هذا المثال في موضوع كيفية القيام بما يلي: الكتابة لملف نصي ( ارشادات البرمجة C# ).


class ReadFromFile
{
    static void Main()
    {
        // The files used here were created in the code example
        // in How to: Write to a Text File. You can of course substitute
        // other files of your own.

        // Example #1
        // Read the file as one string.
        string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");

        // Display the file contents to the console.
        System.Console.WriteLine("Contents of writeText.txt = {0}", text);

        // Example #2
        // Read the file lines into a string array.
        string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");            

        System.Console.WriteLine("Contents of writeLines2.txt =:");
        foreach (string line in lines)
        {
            Console.WriteLine("\t" + line);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}


قم بنسخ التعليمات البرمجية وألصقها في تطبيق وحدة تحكم.

استبدل "c:\testdir" باسم المجلد الفعلي.

و قد تتسبب الحالات التالية باستثناء :

  • الملف غير موجود.

لا تعتمد على اسم الملف لتحديد محتويات الملف. على سبيل المثال، قد لا يكون الملف myFile.cs ملف مصدر لـ #C.

هل وجدت هذا المحتوى مفيدًا؟
(1500 الأحرف المتبقية)

إضافات المجتمع

إضافة
© 1434 Microsoft. All rights reserved.