请问如何在wpf下导入txt文件,能给出个具体的例子吗?

解决方案 »

  1.   

    可以参考这个例子:http://www.cnblogs.com/wpf123/archive/2009/12/20/2347395.html
      

  2.   

    你是要读取TXT内容?文件流读入就好了。
      

  3.   

    private TestTemplate ReadTemplate()
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader("Resources\\TestTemplate.txt");
                }
                catch (System.Exception ex)
                {
                }
                if (sr == null)
                {
                    return;
                }            string strTempContent = sr.ReadToEnd();
                if (!strTempContent.Equals(""))
                {
                    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(strTempContent)))
                    {
                        try
                        {
                            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TestTemplate));
                            testTemplate= (TestTemplate)serializer.ReadObject(ms);
                        }
                        catch (System.Exception ex)
                        {
                        }
                    }
                }
                sr.Close();
            }
    //
    [System.Runtime.Serialization.DataContract]
        public class TestTemplate
        {
            [System.Runtime.Serialization.DataMember]
            public int Key1{ get; set; }        [System.Runtime.Serialization.DataMember]
            public int key2{ get; set; }        [System.Runtime.Serialization.DataMember]
            public int key3 { get; set; }        [System.Runtime.Serialization.DataMember]
            public string KeyValue1{ get; set; }        [System.Runtime.Serialization.DataMember]
            public string KeyValue2{ get; set; }
    [System.Runtime.Serialization.DataMember]
            public string KeyValue3{ get; set; }
        }
    //TestTemplate.txt里内容
    "orderPrintTemplate" : [ {
    "Key1" : "PRINT_TIME",
    "KeyValue1" : "2010-212",
    "Key1" : "PRINT_TIME",
    "KeyValue1" : "2010-212",
    "Key1" : "PRINT_TIME",
    "KeyValue1" : "2010-212", },
    {
    "Key" : "RECEIPT_TITLE",
    "Value" : "领收证",
    }
         ]
    }
      

  4.   

    //TestTemplate.txt里内容
    "orderPrintTemplate" : [{
    [align=right]"Key1" : "PRINT_TIME1",
    "KeyValue1" : "2010-212",
    "Key2" : "PRINT_TIME1",
    "KeyValue2" : "2010-212",
    "Key3" : "PRINT_TIME3",
    "KeyValue3" : "2010-212",[/align]},
    {
    [align=right]"Key1" : "PRINT_TIME1",
    "KeyValue1" : "2010-212",
    "Key2" : "PRINT_TIME1",
    "KeyValue2" : "2010-212",
    "Key3" : "PRINT_TIME3",
    "KeyValue3" : "2010-212",[/align]}
         ]
      

  5.   

    文件流读取!
     public string ReadText(string Path)
            {
                StreamReader sr = new StreamReader(Path, Encoding.Default);
                string readtxt = sr.ReadToEnd();
                sr.Close();
                return readtxt;
            }