从 C# 中的源代码文件中搜索文本框中的字符串

search a string in textbox from a source code file in c#

我是这里的新手。我没有任何经验 coding.I 卡在我 project.I 的编码部分,在 visual studio

中用 C# 完成

我想在打开的文件中的文本框中搜索一个字符串,该字符串作为另一个字符串(源代码)打开。我想检查输入的字符串是否在源代码中的单词“$_POST”前面的 20 个字符之前 code.and 如果字符串在那里,打印一些东西。我已附上代码,直到 done.Please 帮助我。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace XSS_Prevention_Tool
{
    public partial class Home : Form
    {
        int count = 0,lc=0;
        string language;

        public Home()
        {
            InitializeComponent();
        }

        private void Home_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            languageSelection();
            folderBrowserDialog1.ShowDialog();
            txtpath.Text= folderBrowserDialog1.SelectedPath.ToString();
            ColumnHeader header1 = new ColumnHeader();
            header1.Text = " Scanning: ";
            header1.Width = listFiles.ClientSize.Width;
            listFiles.Columns.Add(header1);
            DirSearch(folderBrowserDialog1.SelectedPath.ToString());
        }

        private void languageSelection()
        {
            if (rdoASP.Checked == true)
            {
                language = "asp";
            }
            else if (rdoJSP.Checked == true)
                language = "jsp";
            else if (rdoPHP.Checked == true)
                language = "php";
            else
                MessageBox.Show("Select a language");
            lblLanguage.Text = language;
            textBox2.Visible = true;
        }

        public void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                {
                    string e = Path.GetExtension(f);
                    if (e == "."+language)
                    {
                        listFiles.Items.Add(f);
                        lc++;
                        textBox2.Text = lc.ToString();
                        XSS(f);
                    }
                    count++;
                    textBox1.Text = count.ToString();
                }
                foreach (string d in Directory.GetDirectories(dir))
                {
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void XSS(string f)
        {
            String methode = "$_POST";
            System.IO.StreamReader myFile = new System.IO.StreamReader(f);
            string myString = myFile.ReadToEnd();

            myFile.Close();
            if (myString.Contains(methode))
            {
                int pos = myString.IndexOf(methode);
                MessageBox.Show(pos.ToString());
            }


        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void listFiles_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void txtpath_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }


    }
}

我假设您需要在字符串中找到子字符串。只需浏览这些示例并尝试找到解决方案。

http://www.dotnetperls.com/substring

C# 字符串中的子字符串 Class returns 作为此字符串的子字符串的新字符串。子字符串从指定的给定索引开始并扩展到给定的长度。

字符串string.substring(int startIndex,int length)

参数:

startIndex: 子字符串开始的索引。

length: 子字符串中的字符数。

Returns:

指定子串。

例外情况:

System.ArgumentOutOfRangeException : beginIndex 或长度小于零,或 begin 索引 + 长度不在指定字符串内