⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 这是一本介绍Csharp示例的电子书
💻 CS
📖 第 1 页 / 共 2 页
字号:
                                                                          this.chkNotListAll,
                                                                          this.lblResults,
                                                                          this.txtFiles,
                                                                          this.lblFiles,
                                                                          this.btnSearch,
                                                                          this.ckInclude,
                                                                          this.txtResults,
                                                                          this.lblSearchText,
                                                                          this.lblDir,
                                                                          this.btnBrowse,
                                                                          this.txtDir,
                                                                          this.txtSearchText});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "文件内容搜索器";
            this.ResumeLayout(false);

        }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

        private void txtFiles_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter && btnSearch.Enabled==true)
            {
                Search();
            }
        }

        private void txtDir_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter && btnSearch.Enabled==true)
            {
                Search();
            }
        }

        private void txtSearchText_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Enter && btnSearch.Enabled==true)
            {
                Search();
            }
        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            Search();
        }

        private void btnBrowse_Click(object sender, System.EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Select a file";
            //fdlg.InitialDirectory = DirectoryInfo.CurrentDirectory;
            fdlg.Filter = "All files (*.*)|*.*";
            if(fdlg.ShowDialog() == DialogResult.OK)
            {
                String strPath = fdlg.FileName;
                //File Extension
                String strExt;
                //Get the Directory and file extension
                FileInfo f = new FileInfo(strPath);
                txtDir.Text = f.DirectoryName;
                strExt = f.Extension;
                //Eliminate the first '.'
                if(strExt != "")
                    strExt = strExt.Substring(1);
                txtFiles.Text = strExt;
            }
        }

        private void txtDir_TextChanged(object sender, System.EventArgs e)
        {
            VerifySearchBtn();
        }

        private void txtSearchText_TextChanged(object sender, System.EventArgs e)
        {
            VerifySearchBtn();
        }

        protected void VerifySearchBtn()
        {
            if(txtDir.Text != "" && txtSearchText.Text != "")
            {
                btnSearch.Enabled = true;
            }
            else
                btnSearch.Enabled = false;
        }

        protected void GetFiles(String strDir, String strExt, bool bRecursive)
        {
            DirectoryInfo dir = new DirectoryInfo(strDir);
            FileInfo[] fileList = dir.GetFiles("*." + strExt);
            for(int i=0; i<fileList.Length; i++)
            {
                if(fileList[i].Exists)
                    m_arrFiles.Add(strDir + "\\" + fileList[i].Name);
            }
            if(bRecursive==true)
            {
                //Get recursively from subdirectories
                DirectoryInfo[] dirList = dir.GetDirectories();
                for(int i=0; i<dirList.Length; i++)
                {
                    GetFiles(strDir + "\\" + dirList[i].Name, strExt, bRecursive);
                }
            }
        }

        //Search Function
        protected void Search()
        {
            String strDir = txtDir.Text;
            //Check First if the Selected Directory exists
            DirectoryInfo dir = new DirectoryInfo(strDir);
            if(!dir.Exists)
                MessageBox.Show("Directory doesn't exist!", "Win Grep Error");
            else
            {
                Text = "文件内容搜索器 " + strDir;
                Cursor = System.Windows.Forms.Cursors.WaitCursor;
                //File Extension
                String strExt = txtFiles.Text;
                if(strExt != "")
                    if(strExt.StartsWith("."))
                    {
                        //Eliminate the first '.'
                        strExt = strExt.Substring(1);
                    }
                //First empty the list
                m_arrFiles.Clear();
                //Create recursively a list with all the files complying with the criteria
                GetFiles(strDir, strExt, ckInclude.Checked);
                //Now all the Files are in the ArrayList, open each one
                //iteratively and look for the search string
                String strSearch = txtSearchText.Text;
                String strResults = "";
                String strLine;
                int iLine;
                IEnumerator enm = m_arrFiles.GetEnumerator();
                while(enm.MoveNext())
                {
                    try
                    {
                        StreamReader sr = File.OpenText((string)enm.Current);                        
                        iLine = 0;
                        string strResultsInThisFile = "";
                        while((strLine = sr.ReadLine())!=null)
                        {
                            iLine++;
                            //Search the Line for the Search Text
                            if(strLine.IndexOf(strSearch) != -1)
                            {
                                //Add the Line to Results string
                                strResultsInThisFile += "  " + iLine + ": " + strLine + "\r\n";
                            }	
                        }
                        sr.Close();
                        
                        if(chkNotListAll.Checked)
                        {
                            if(strResultsInThisFile.Length > 0)
                            {
                                strResults += "\r\n" + (string)enm.Current + ":\r\n";
                                strResults += strResultsInThisFile;
                            }
                        }
                        else
                        {
                            strResults += "\r\n" + (string)enm.Current + ":\r\n";
                            strResults += strResultsInThisFile;
                        }
                    }
                    catch(SecurityException)
                    {
                        strResults += "\r\n" + (string)enm.Current + ": Security Exception\r\n\r\n";	
                    }
                    //catch(AccessException)
                    //{
                    //	strResults += "\r\n" + (string)enm.Current + ": Access Exception\r\n";
                    //}
                }
                txtResults.Text = strResults;
                Cursor = System.Windows.Forms.Cursors.Arrow;
            }
        }

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -