Answer the question
In order to leave comments, you need to log in
How in C# to organize a search in a file and assign strings to the fields of an object?
Hello. There is a class that creates book objects, with fields: authors (array), title, publisher, year of publication, date of issue of the book, return date. I uploaded all these data to the file, I will give the code below. The question is how to organize the output of this information, at least by name, I will further improve it. Is it better to search for a line in a file and then output the next 5 lines, or to throw the entire file into an array of objects and search there already?
Here is the code that is already there (by the way, everything is on the form):
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
string path = @"U:\Study\Практика по программированию\Прога\Date.txt";
public Form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
Book book = new Book(textBox1.Text.Split(' '), textBox2.Text, textBox3.Text, Int32.Parse(textBox4.Text), dateTimePicker1.Value, dateTimePicker2.Value);
GetInfo(book);
using (FileStream fstream = new FileStream($"{path}", FileMode.Append, FileAccess.Write))
{
string Author = "";
for (int i = 0; i < book.Author_name.Length; i++)
{
Author += book.Author_name[i].ToString() + " ";
}
byte[] input = Encoding.Default.GetBytes(Author + "\n" + book.Book_name.ToString() + "\n" + book.Publishing_house.ToString() + "\n" + book.Year.ToString() + "\n" + book.Date_of_issue.ToString() + "\n" + book.Repayment_period.ToString() + "\n" + "\n");
fstream.Write(input, 0, input.Length);
}
}
public void button2_Click(object sender, EventArgs e)
{
Book book = new Book();
string text = "";
using (StreamReader fs = new StreamReader(path))
{
while (true)
{
// Читаем строку из файла во временную переменную.
string temp = fs.ReadLine();
// Если достигнут конец файла, прерываем считывание.
if (temp == null) break;
// Пишем считанную строку в итоговую переменную.
if (textBox5.Text == temp) {
}
}
}
}
public void GetInfo(Book b)
{
textBox7.Text = "";
for (int i = 0; i < b.Author_name.Length; i++)
{
textBox7.Text += b.Author_name[i].ToString() + "\r\n";
}
textBox8.Text = b.Book_name.ToString();
textBox9.Text = b.Publishing_house.ToString();
textBox10.Text = b.Year.ToString();
dateTimePicker3.Value = b.Date_of_issue;
dateTimePicker4.Value = b.Repayment_period;
}
}
public class Book
{
public string[] Author_name { get; set; }
public string Book_name { get; set; }
public string Publishing_house { get; set; }
public int Year { get; set; }
public DateTime Date_of_issue { get; set; }
public DateTime Repayment_period { get; set; }
public Book(){}
public Book(string n)
{
Book_name = n;
}
public Book(string n, int y)
{
Book_name = n;
Year = y;
}
public Book(string[] a, string n, string p, int y, DateTime d, DateTime r)
{
Author_name = a;
Book_name = n;
Publishing_house = p;
Year = y;
Date_of_issue = d;
Repayment_period = r;
}
}
}
Answer the question
In order to leave comments, you need to log in
Look, I was looking for information on the search yesterday, I don’t remember exactly now, but it looks like your case.
1. You need to enable indexing on the disk where you store files.
2. When creating each file, you give it a Meta attribute, according to your fields in the class.
3. Use the standard Windows Search API
for searching 4. Save the search result in an ObjectDataSource
5. Place a DataGrid on the form and bind it to the ObjectDataSource
6. Use the "Output data" button to compose a query, execute it, fill in the ObjectDataSource
Link to a minimal example here
https ://stackoverflow.com/questions/34338465/how-t...
I advise you to study https://docs.microsoft.com/en-us/dotnet/csharp/pro...
Use built-in serialization.
1. To do this, change the properties to fields, add the attribute
[Serializable()]
public class Book
{
public string[] Author_name;
...
}
var formatter = new SoapFormatter();
using (var fileStream = File.Create(fileName)) formatter.Serialize(fileStream, book1);
public class Library {
public Book Books[];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question