且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

搜索文本文件 c#

更新时间:2023-02-20 11:30:55

首先,给你目前班级的一些建议...

Save 功能对于再次从文件中提取数据非常糟糕.改为这样做:

The Save function is exceptionally poor for pulling out data from the file again. Do this instead:

public class Product {
    // Note that I've added a constructor for this class - this'll help later
    public Product(string productName, string customerName, string firmwareLocation) {
        this.productName = productName;
        this.customerName = customerName;
        this.firmwareLocation = firmwareLocation;
    }

    public void Save (System.IO.TextWriter textOut)
    {
        textOut.WriteLine(String.Format(
            "{0},{1},{2}", this.productName, this.customerName, this.firmwareLocation);
    }
}

所以,而不是得到这个:

So instead of getting this:

...
Awesome Hairdryer
Nick Bull
C://hair.firmware
Awesome TV
Nick Bull
C://tv.firmware
...

你明白了:

...
Awesome Hairdryer,Nick Bull,C://hair.firmware
Awesome TV,Nick Bull,C://tv.firmware
...

一旦你这样做了......

这是一个非常简单的问题.一个班轮,如果你想要的话,还有一些用作搜索"过滤器的方法示例:

It's a really easy question. One liner, with some examples of methods used as filters for "searching", if you want them:

IEnumerable<string> lines = File.ReadLines(pathToTextFile)
    .TakeWhile(line => line.Contains("Nick Bull"));

EDIT:更简洁的单行代码,返回一个 Product 集合:

EDIT: Even neater one-liner, returning a Product collection:

List<Product> lines = File.ReadLines(pathToTextFile)
    .TakeWhile(line => line.Contains("Nick Bull"))
    .Select(line => new Product(line.Split(',')[0], line.Split(',')[1], line.Split(',')[2])
    .ToList();

要遍历它们并执行其他更复杂的操作,您可以全部阅读它们然后执行操作:

To iterate through them and do other more complicated stuff, you could read them all then do stuff:

var lines = File.ReadAllLines(filePath);
var products = new List<Product>();

foreach (string line in lines) {
    if (Regex.IsMatch(line, @"super awesome regex")) {
        string[] lineItems = line.Split(','); // Splits line at commas into array
        products.Add(new Product(line[0], line[1], line[2]); // Thanks to our constructor
    }
}

foreach (var product in products) {
    Console.WriteLine(String.Format("Product name: {0}", product.productName));
}

搜索功能更新

要搜索,请使用以下功能:

To search, use these functions:

public enum ProductProperty {
    ProductName,
    CustomerName,
    FirmwareLocation
}

List<Product> GetAllProductsFromFile(string filePath) {
    if (!File.Exists(filePath)) throw FileNotFoundException("Couldn't find " + filePath);

    return File.ReadLines(filePath)
       .Select(line => new Product(line.Split(',')[0], line.Split(',')[1], line.Split(',')[2])
        .ToList();
}

function SearchProductsByProperty(IEnumerable<Product> products, ProductProperty productProperty, string value) {
    return products.ToList().Where(product => 
        (productProperty == ProductProperty.ProductName) ? product.productName == productName :
        (productProperty == ProductProperty.CustomerName) ? product.customerName == customerName :
        (productProperty == ProductProperty.FirmwareName) ? product.firmwareName == firmwareName : throw new NotImplementedException("ProductProperty must be ProductProperty.ProductName, ProductProperty.CustomerName or ProductProperty.FirmwareName");
    );
}

那么:

var products = GetAllProductsFromFile(filePath);
var searchedProducts = SearchProductsByProperty(products, ProductProperty.ProductName, "Awesome TV");

foreach (var product in searchedProducts) {
    // Each product will have a `ProductName` equal to "Awesome TV".
    // Finally, get the customer name by doing this within this foreach loop, using `product.customerName`
    Console.WriteLine(product.customerName);
}