在程序运行时,将文本文件的内容作为文本框的输入

时间:2015-03-02 15:39:31

标签: c# .net textbox scraper

我有一个保存在指定文件夹中的输入文件,并使用流阅读器一次读取一行内容。所以我的程序读取第一行并在文本框中添加内容。我想得到文本文件的第一行,并通过文本框将其用作程序的输入。所以我的问题是如何读取从输入文件中显示的文本框内容并将其用作程序的输入。

            string inputFile = @"c:\temp\ScrappedData\input.txt";
            Numbers(inputFile);

            //gets the contents of the input file loaded by Numbers method
            string lakeName = textBox1.Text;

            string partialWebAdd = "http://www.google.com?=";

            //Gets the url entered by the user from the textbox
            string url = partialWebAdd + lakeName;
            MessageBox.Show(url.ToString());
            //string url = textBox1.Text;

因此,Numbers(inputFile)从input.txt加载第一行(这是与网站中的数据匹配的一些数值),网站地址显示良好。但是当我真的试图刮掉它时,webclient没有读取文本框的输入。你知道为什么吗? 我尝试使用相同的代码而不从文本文件中输入,而是将整个网站数据输入到框中,它工作正常。 谢谢你的想法。 下面是Number Method和webrequest的代码部分。

string sourceCode = GetSource.getSourceCode(url);



            //Marks the start point of scrape 
            int startIndex = sourceCode.IndexOf("Name:");

            //Marks the endpoint of the html to scrape
            int endIndex = sourceCode.IndexOf("For more information");

            //Gets the string between the specified startIndex and endIndex
            MessageBox.Show(startIndex.ToString()+ "dang bro" +endIndex.ToString());
            sourceCode = sourceCode.Substring(startIndex, endIndex - startIndex);





            //HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            //Write the contents of the webpage to the file specified in path #162
            StreamWriter sWriter = new StreamWriter(path);
            sWriter.Write(sourceCode);
            MessageBox.Show("Contents have been Scrapped!");

            textBox1.Clear();
            sWriter.Close();

Webrequest课程:

class GetSource
{
    public static string getSourceCode(string url)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader streamReader = new StreamReader(response.GetResponseStream());
        string sourceCode = streamReader.ReadToEnd();

        streamReader.Close();
        response.Close();
        return sourceCode;
    }
}

}

数字方法:

public void Numbers(string filename)
   {


       string input;

       if (System.IO.File.Exists(filename) == true)
       {
           System.IO.StreamReader objectReader;
           objectReader = new System.IO.StreamReader(filename);
           while ((input = objectReader.ReadLine()) != null)
           {
               Single output;
               if (Single.TryParse(input, out output))
               {
                   textBox1.AppendText(output.ToString());
                   MessageBox.Show("input to the textbox from input.txt was successful");


               }
               else
               {

               }
           }
           objectReader.Close();

       }
       else
       {
           MessageBox.Show("No Such File" + filename);
       }
   }

0 个答案:

没有答案