【问题标题】:search a string in textbox from a source code file in c#从 C# 中的源代码文件中搜索文本框中的字符串
【发布时间】:2015-07-12 06:02:42
【问题描述】:

我是这里的新手。而且我没有任何编码经验。我被困在我项目的编码部分。我在 Visual Studio 的 C# 中完成了它

我想在作为源代码的另一个字符串打开的文件中搜索文本框中键入的字符串。我想在源代码中的“$_POST”这个词前面的 20 个字符之前检查输入的字符串是否存在。如果字符串存在,打印一些东西。我已经附上了代码,直到我完成为止。请帮助我。

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# string visual-studio search substring


【解决方案1】:

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

http://www.dotnetperls.com/substring

C# 字符串中的子字符串类返回一个新字符串,该字符串是该字符串的子字符串。子字符串从指定的给定索引开始并一直延伸到给定的长度。

string string.substring(int startIndex,int length)

参数:

startIndex:子串开始的索引。

length:子串中的字符数。

返回:

指定的子字符串。

例外:

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

【讨论】:

  • 感谢 BSG,很高兴至少有人理解我的问题。我会尽力。你也可以告诉我如何从文本框中搜索它
  • 将 textbox.Text 值存储在字符串中,您可以使用此字符串进行搜索。字符串 s = textBox1.text;
  • 如果您认为堆栈溢出成员提供的任何解决方案对您有用,请支持该解决方案,如果该解决方案有效,请接受答案。谢谢 BSG。
猜你喜欢
  • 2012-10-01
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 2021-12-20
  • 1970-01-01
相关资源
最近更新 更多