C# 在程序运行时,将文本文件的内容作为文本框的输入
我将一个输入文件保存在指定的文件夹中,并使用流读取器一次读取一行内容。因此,我的程序读取第一行并将内容添加到文本框中。我想获取文本文件的第一行,并通过textbox将其用作程序的输入。所以我的问题是如何读取从输入文件显示的文本框内容,并将其用作程序的输入C# 在程序运行时,将文本文件的内容作为文本框的输入,c#,.net,textbox,scraper,C#,.net,Textbox,Scraper,我将一个输入文件保存在指定的文件夹中,并使用流读取器一次读取一行内容。因此,我的程序读取第一行并将内容添加到文本框中。我想获取文本文件的第一行,并通过textbox将其用作程序的输入。所以我的问题是如何读取从输入文件显示的文本框内容,并将其用作程序的输入 string inputFile = @"c:\temp\ScrappedData\input.txt"; Numbers(inputFile); //gets the
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方法和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);
}
}
显示您使用WebClient的代码。我将添加代码,但当我尝试手动输入时,它工作正常。问题是程序没有从文本文件读取输入。例如,我尝试在文本文件中复制完整的网址。但是没有从文本框读取输入。因此,问题在于textbox在运行时不接受作为输入加载的内容。如果有帮助的话,我会发布代码。谢谢,您的问题描述不清楚。读取文本框的文本没有什么特别的,它只是一个对象的字符串属性。如果问题是在读取文件时,请显示您的方法的内容(如果我可以说名称不正确)
Numbers()
方法。我已添加了您要求的代码。