找到你要的答案

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

Q:搜索从C #源代码文件在文本字符串

I'm a newbie to here. and i don't have any expirience in coding.I was stuck in coding part of my project.I have done it in C# in visual studio

I wanted to search a string typed in text box in a file that is open as another string which is a source code. And i wanted to check the typed string is there before a 20 characters in front of the word "$_POST" which is in the source code.and if the string is there, print something. I have attached the code until i have done.Please help me.

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)
        {

        }


    }
}

我是一个新手,在这里。我没有任何经验的编码。我被困在编码我的项目的一部分。我在C # Visual Studio做

我想搜索一个在文本框中键入的字符串,该文件打开为另一个字符串,这是一个源代码。我想检查输入的字符串是在20字前面的字“$ _post”这是源代码的字符串,如果有,打印的东西。我已经附上了代码,直到我做了,请帮助我。

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)
        {

        }


    }
}
answer1: 回答1:

I assume you need to find the substring within a string. Just go through these examples and try to find a solution.

http://www.dotnetperls.com/substring

Substring in C# string Class returns a new string that is a substring of this string. The substring begins at the specified given index and extended up to the given length.

string string.substring(int startIndex,int length)

Parameters:

startIndex: The index of the start of the substring.

length: The number of characters in the substring.

Returns:

The specified substring.

Exceptions:

System.ArgumentOutOfRangeException : the beginIndex or length less than zero, or the begin index + length not within the specified string

我认为你需要在字符串中查找子字符串。只是通过这些例子,并试图找到一个解决方案。

http://www.dotnetperls.com/substring

C #子字符串类返回一个字符串,这个字符串的字符串。子字符串在指定的特定指标和延长到给定的长度开始。

串。串(int startIndex int length)

参数:

指数:对字符串开始的指标。

长度:在子字符串的字符数。

返回:

指定的子字符串。

例外:

system.argumentoutofrangeexception:的beginIndex或长度小于零,或者开始指数+长度不指定字符串中

c#  string  visual-studio  search  substring