当前位置:编程学习 > C#/ASP.NET >>

怎样获取导入的txt文档内容

 1,在程序中我设定了一个Button
 2,点击时出来文件选择对话框
 3,选择一个txt文件并点确定后



问题:我怎样获取此txt文件中的内容? 比如我想把这个txt文件中的内容获取出来用string变量储存。

    语言:C#。

谢谢。
--------------------编程问答--------------------
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // This text is added only once to the file.
        if (!File.Exists(path))
        {
            // Create a file to write to.
            string createText = "Hello and Welcome" + Environment.NewLine;
            File.WriteAllText(path, createText, Encoding.UTF8);
        }

        // This text is always added, making the file longer over time
        // if it is not deleted.
        string appendText = "This is extra text" + Environment.NewLine;
        File.AppendAllText(path, appendText, Encoding.UTF8);

        // Open the file to read from.
        string readText = File.ReadAllText(path, Encoding.UTF8);
        Console.WriteLine(readText);
    }
}
--------------------编程问答-------------------- 直接用File.ReadAllText()读取不可以么?

string content = File.ReadAllText(path);
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,