有没有办法在 File.ReadAllLines 方法中使用通配符 (*)?

Is there a way use a wildcard operator(*) in the File.ReadAllLines method?

我正在尝试根据 Microsoft 的教程将多个 CSV 文件多次转换为 XML:

// Create the text file.  
string csvString = @"GREAL,Great Lakes Food Market,Howard Snyder,Marketing Manager,(503) 555-7555,2732 Baker Blvd.,Eugene,OR,97403,USA  
HUNGC,Hungry Coyote Import Store,Yoshi Latimer,Sales Representative,(503) 555-6874,City Center Plaza 516 Main St.,Elgin,OR,97827,USA  
LAZYK,Lazy K Kountry Store,John Steel,Marketing Manager,(509) 555-7969,12 Orchestra Terrace,Walla Walla,WA,99362,USA  
LETSS,Let's Stop N Shop,Jaime Yorres,Owner,(415) 555-5938,87 Polk St. Suite 5,San Francisco,CA,94117,USA";  
File.WriteAllText("cust.csv", csvString);  

// Read into an array of strings.  
string[] source = File.ReadAllLines("cust.csv");  
XElement cust = new XElement("Root",  
    from str in source  
    let fields = str.Split(',')  
    select new XElement("Customer",  
        new XAttribute("CustomerID", fields[0]),  
        new XElement("CompanyName", fields[1]),  
        new XElement("ContactName", fields[2]),  
        new XElement("ContactTitle", fields[3]),  
        new XElement("Phone", fields[4]),  
        new XElement("FullAddress",  
            new XElement("Address", fields[5]),  
            new XElement("City", fields[6]),  
            new XElement("Region", fields[7]),  
            new XElement("PostalCode", fields[8]),  
            new XElement("Country", fields[9])  
        )  
    )  
);  
Console.WriteLine(cust);

我打算使用 foreach 循环遍历目录中需要转换的所有 CSV 文件,但遇到了麻烦。

行中:

string[] source = File.ReadAllLines("cust.csv");

是否有办法将 cust.csv 替换为类似通配符 (*.csv) 的方法,以便它获取其运行的所有 CSV 文件,例如:

string[] source = File.ReadAllLines("*.csv");

我知道上面的方法不起作用,因为 ReadAllLines 不支持“*”,但是这样做的逻辑是什么,以便循环遍历所有 CSV 文件,然后对它们应用逻辑转换?

but what would be the logic for this so that it loops through all CSV files, then applies the logic conversion to them?

您首先使用Directory.GetFiles or Directory.EnumerateFiles 列出目录中的相关文件。 都支持通配符。

获得要操作的文件后,您可以依次读取每个文件。