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

C#(winform) 操作.txt文本的例子

 

背景:一个可输入textbox,可以实现搜索功能

功能改进需求:把textbox改造成一个可输入的下拉框,下拉框的内容是历史搜索记录(最近20次输入搜索的内容)

www.zzzyk.com

代码:

 

using System.IO;

using System.Text.RegularExpressions;

 

private ArrayList _arrSearchHistory = null; // 全局变量  form_load时读取.txt文本中的内容 

 

// 放在form_load中 

ReadTxtFile(out _arrSearchHistory); 

if (_arrSearchHistory != null && _arrSearchHistory.Count > 0) 

    SetComboList(cboFilter, _arrSearchHistory, null, "", false, true); 

 

// 加到搜索功能中 

if (!string.IsNullOrEmpty(cboFilter.Text) && cboFilter.Text.Trim().Length>1) 

     AddItemToTxt(System.Windows.Forms.Application.StartupPath.Replace(@"bin\Debug", "") + "/Data/SearchHistory.txt", cboFilter.Text,out _arrSearchHistory); 

 

//公共函数部分 

     private void ReadTxtFile(out ArrayList arrFile,int iTopRow=20) 

     { 

         arrFile = new ArrayList(); 

         try 

         { 

             string sFileFullName = System.Windows.Forms.Application.StartupPath.Replace(@"bin\Debug", "") + @"/Data"; 

             if (!System.IO.Directory.Exists(sFileFullName)) 

             { 

                 System.IO.Directory.CreateDirectory(sFileFullName); 

             } 

 

             sFileFullName += @"/SearchHistory.txt"; 

             if (!System.IO.File.Exists(sFileFullName)) 

             { 

                 //System.IO.File.CreateText(sFileName); 

                 FileStream fs; 

                 fs = File.Create(sFileFullName); 

                 fs.Close(); 

             } 

 

             FileStream fsTxtFile = new FileStream(sFileFullName, FileMode.Open, FileAccess.Read); 

             StreamReader sr = new StreamReader(fsTxtFile, System.Text.Encoding.Default); 

             string sTemp; 

 

             sTemp = sr.ReadLine(); 

             int iTempRow=0; 

             while (!string.IsNullOrEmpty(sTemp)) 

             { 

                 iTempRow++; 

                 if (iTempRow > iTopRow) 

                 { 

                     break; 

                 } 

                 arrFile.Add(sTemp); 

                 sTemp = sr.ReadLine(); 

             } 

 

             sr.Close(); 

             sr.Dispose(); 

             fsTxtFile.Dispose(); 

         } 

         catch (Exception ex) 

         { 

             Common.DisplayMsg(this.Text, ex.Message.ToString()); 

         } 

     } 

 

     /// <summary> 

     /// 计算字符串中子串出现的次数 

     /// </summary> 

     /// <param name="str">字符串</param> 

     /// <param name="substring">子串</param> 

     /// <returns>出现的次数</returns> 

     public  static int SubstringCount(string str, string substring) 

     { 

         if (str.Contains(substring)) 

         { 

             string strReplaced = str.Replace(substring, ""); 

             return (str.Length - strReplaced.Length) / substring.Length; 

         } 

 

         return 0; 

     } 

 

     private void AddItemToTxt(string sFileFullName, string sNewItem,out ArrayList arrFile) 

     { 

         arrFile = _arrSearchHistory; 

 

         try 

         { 

             string sTemp=""; 

             using (StreamReader sr = new StreamReader(sFileFullName)) 

  &

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,