C# 如何循环遍历目录C中的所有文本文件#
这段代码从1.txt中获取一行并将其拆分为列。现在我有一个200多个文件的目录,结尾是something.txt,我希望它们一次打开一个,然后运行下面的过程。在不太修改代码的情况下循环所有文件的最简单方法是什么 当前的代码片段C# 如何循环遍历目录C中的所有文本文件#,c#,.net,ssis,C#,.net,Ssis,这段代码从1.txt中获取一行并将其拆分为列。现在我有一个200多个文件的目录,结尾是something.txt,我希望它们一次打开一个,然后运行下面的过程。在不太修改代码的情况下循环所有文件的最简单方法是什么 当前的代码片段 string _nextLine; string[] _columns; char[] delimiters; delimiters = "|".ToCharArray(); _nextLine = _reader.Read
string _nextLine;
string[] _columns;
char[] delimiters;
delimiters = "|".ToCharArray();
_nextLine = _reader.ReadLine();
string[] lines = File.ReadAllLines("C:\\P\\DataSource2_W\\TextFiles\\Batch1\\1.txt");
//Start at index 2 - and keep looping until index Length - 2
for (int i = 3; i < lines.Length - 2; i++)
{ _columns = lines[i].Split('|');
// Check if number of cols is 3
if (_columns.Length == 146)
{
JazzORBuffer.AddRow();
JazzORBuffer.Server = _columns[0];
JazzORBuffer.Country = _columns[1];
JazzORBuffer.QuoteNumber = _columns[2];
JazzORBuffer.DocumentName =_columns[3];
JazzORBuffer.CompanyNameSoldTo=_columns[4];
}
else
{
// Debug or messagebox the line that fails
MessageBox.Show("Cols:" + _columns.Length.ToString() + " Line: " + lines[i]);
return;
}
}
string\u nextLine;
字符串[]_列;
字符[]分隔符;
分隔符=“|”。.ToCharArray();
_nextLine=_reader.ReadLine();
string[]lines=File.ReadAllLines(“C:\\P\\DataSource2\u W\\TextFiles\\Batch1\\1.txt”);
//从索引2开始-并保持循环,直到索引长度为-2
对于(int i=3;i
您可以简单地使用来迭代指定目录的文件集合
因此,您可以在foreach循环中插入代码,如:
foreach (var file in
Directory.EnumerateFiles(@"C:\\P\\DataSource2_W\\TextFiles\\Batch1", "*.txt"))
{
//your code
}
由于microsoft错误,必须将其更改为此。foreach(Directory.GetFiles(@“C:\\P\\DataSource2\U W\\TextFiles\\Batch1\\”,“*.txt”)中的var文件){感谢您的回复。非常感谢。