Search i tekstfil
Jeg har lavet en aspx-side, som benytter en ".cs" fil (som jeg også har lavet). På aspx-sden skriver man en streng ind, som .cs-filen søger et tekstdokument igennem for. Alt afhængig af, om der returneres true eller false, så skal der stå noget tekst i en andet tekstbox. Men det fungerer ikke.Sådan ser flerne ud:
Aspx:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace New
{
/// <summary>
/// Summary description for MovieSearch.
/// </summary>
public class MovieSearch : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
MovieLed film;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
film=new MovieLed();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.TextBox2.TextChanged += new System.EventHandler(this.TextBox2_TextChanged);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
}
private void Button1_Click(object sender, System.EventArgs e)
{
TextBox2.Text.Insert(0, "Testing");
bool svar=film.search(TextBox1.Text.ToString());
TextBox2.Visible=true;
if(svar==true)
TextBox2.Text.Insert(0, "I've got the movie. Give me a message through ICQ or something and I'll put it in my share!");
if(svar==false)
TextBox2.Text.Insert(0, "Sorry - no luck this time!");
}
private void TextBox2_TextChanged(object sender, System.EventArgs e)
{
}
}
}
cs:
using System;
using System.IO;
using System.Text;
namespace New
{
public class MovieLed
{
bool found=false;
public MovieLed()
{Console.Write("MovieLed begyndt!");}
public bool search(string title)
{
Console.WriteLine("Searchttle er ved start: " + title);
try
{
using (StreamReader sr = new StreamReader("G:\\FromOldHardDrive\\Movies\\Movies.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
line.Insert(0, "-");
if(line==title)
found=true;
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
Console.WriteLine("Found før return er: " + found);
return found;
//læs filmdokument ind, og led efter "title"
//string path=@"G:\\FromOldHardDrive\\Movies\\Movies.txt";
//using (FileStream fs=new File.OpenRead(path))
//{
//fs.
//}
//FileStream fs=new FileStream(@"c:\test.txt, FileMode.Open);
//BufferedStream bs=new BuferedStream(fs);
}
void main()
{
MovieLed a=new MovieLed();
Console.Write(a.search("About A Boy"));
}
}
}
Nogle gode forslag til løsning?