在 C# 中从文本文件创建数组列表
Create list of arrays from text file in C#
我有许多文本文件都遵循相同的内容格式:
"Title section","Version of the app"
10
"<thing 1>","<thing 2>","<thing 3>","<thing 4>","<thing 5>","<thing 6>","<thing 7>","<thing 8>","<thing 9>","<thing 10>"
'Where:
' first line never changes, it always contains exactly these 2 items
' second line is a count of how many "line 3s" there are
' line 3 contains a command to execute and (up to) 9 parameters
' - there will always be 10 qoute-delimited entries, even if some are blank
' - there can be N number of entries (in this example, there will be 10 commands to read)
我正在使用 StreamReader 读取其中的每个文本文件,并希望将每个文件设置为自己的 class。
public class MyTextFile{
public string[] HeaderLine { get; set; }
public int ItemCount { get; set; }
List<MyCommandLine> Commands { get; set;}
}
public class MyCommandLine{
public string[] MyCommand { get; set; }
}
private void btnGetMyFilesiles_Click(object sender, EventArgs e){
DirectoryInfo myFolder = new DirectoryInfo(@"C:\FileSpot");
FileInfo[] myfiles = myfolder.GetFiles("*.ses");
string line = "";
foreach(FileInfo file in Files ){
str = str + ", " + file.Name;
// Read the file and display it line by line.
System.IO.StreamReader readingFile = new System.IO.StreamReader(file.Name);
MyTextFile myFileObject = new MyTextFile()
while ((line = readingFile.ReadLine()) != null){
' create the new MyTextFile here
}
file.Close();
}
}
}
objective是判断实际调用的命令是什么(""),如果其余参数中有任何一个指向一个预先存在的文件,则判断该文件是否存在。我的问题是我无法弄清楚如何将 N 个 "line 3" 读入它们自己的对象并将这些对象附加到 MyTextFile 对象。我 99% 确定我在逐行阅读每个文件时误入歧途,但我不知道如何摆脱它。
如果所有的都用逗号分隔 ,
你可以这样做:
int length = Convert.ToInt32 (reader.ReadLine ());
string line = reader.ReadLine ();
IEnumerable <string> things = line.Split (',').Select (thing => thing. Replace ('\"'', string.Empty).Take(length);
Take
表示从行中取多少things
。
因此,解决将 N 个第 3 行项目放入 class 的具体问题,您可以执行类似的操作(显然您可以进行一些更改,以便它更适合您的应用程序)。
public class MyTextFile
{
public List<Array> Commands = new List<Array>();
public void EnumerateCommands()
{
for (int i = 0; i < Commands.Count; i++)
{
foreach (var c in Commands[i])
Console.Write(c + " ");
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
string line = "";
int count = 0;
MyTextFile tf = new MyTextFile();
using (StreamReader sr = new StreamReader(@"path"))
{
while ((line = sr.ReadLine()) != null)
{
count += 1;
if (count >= 3)
{
object[] Arguments = line.Split(',');
tf.Commands.Add(Arguments);
}
}
}
tf.EnumerateCommands();
Console.ReadLine();
}
}
至少现在您的 'MyTextFile' class 中有一个命令列表,您可以通过这些命令进行枚举和执行操作。
** 我添加了 EnumerateCommands 方法,以便您可以实际看到列表正在存储行项目。控制台应用程序中的代码应 运行 具有适当的 'using' 语句。
希望对您有所帮助。
我有许多文本文件都遵循相同的内容格式:
"Title section","Version of the app"
10
"<thing 1>","<thing 2>","<thing 3>","<thing 4>","<thing 5>","<thing 6>","<thing 7>","<thing 8>","<thing 9>","<thing 10>"
'Where:
' first line never changes, it always contains exactly these 2 items
' second line is a count of how many "line 3s" there are
' line 3 contains a command to execute and (up to) 9 parameters
' - there will always be 10 qoute-delimited entries, even if some are blank
' - there can be N number of entries (in this example, there will be 10 commands to read)
我正在使用 StreamReader 读取其中的每个文本文件,并希望将每个文件设置为自己的 class。
public class MyTextFile{
public string[] HeaderLine { get; set; }
public int ItemCount { get; set; }
List<MyCommandLine> Commands { get; set;}
}
public class MyCommandLine{
public string[] MyCommand { get; set; }
}
private void btnGetMyFilesiles_Click(object sender, EventArgs e){
DirectoryInfo myFolder = new DirectoryInfo(@"C:\FileSpot");
FileInfo[] myfiles = myfolder.GetFiles("*.ses");
string line = "";
foreach(FileInfo file in Files ){
str = str + ", " + file.Name;
// Read the file and display it line by line.
System.IO.StreamReader readingFile = new System.IO.StreamReader(file.Name);
MyTextFile myFileObject = new MyTextFile()
while ((line = readingFile.ReadLine()) != null){
' create the new MyTextFile here
}
file.Close();
}
}
}
objective是判断实际调用的命令是什么(""),如果其余参数中有任何一个指向一个预先存在的文件,则判断该文件是否存在。我的问题是我无法弄清楚如何将 N 个 "line 3" 读入它们自己的对象并将这些对象附加到 MyTextFile 对象。我 99% 确定我在逐行阅读每个文件时误入歧途,但我不知道如何摆脱它。
如果所有的都用逗号分隔 ,
你可以这样做:
int length = Convert.ToInt32 (reader.ReadLine ());
string line = reader.ReadLine ();
IEnumerable <string> things = line.Split (',').Select (thing => thing. Replace ('\"'', string.Empty).Take(length);
Take
表示从行中取多少things
。
因此,解决将 N 个第 3 行项目放入 class 的具体问题,您可以执行类似的操作(显然您可以进行一些更改,以便它更适合您的应用程序)。
public class MyTextFile
{
public List<Array> Commands = new List<Array>();
public void EnumerateCommands()
{
for (int i = 0; i < Commands.Count; i++)
{
foreach (var c in Commands[i])
Console.Write(c + " ");
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
string line = "";
int count = 0;
MyTextFile tf = new MyTextFile();
using (StreamReader sr = new StreamReader(@"path"))
{
while ((line = sr.ReadLine()) != null)
{
count += 1;
if (count >= 3)
{
object[] Arguments = line.Split(',');
tf.Commands.Add(Arguments);
}
}
}
tf.EnumerateCommands();
Console.ReadLine();
}
}
至少现在您的 'MyTextFile' class 中有一个命令列表,您可以通过这些命令进行枚举和执行操作。
** 我添加了 EnumerateCommands 方法,以便您可以实际看到列表正在存储行项目。控制台应用程序中的代码应 运行 具有适当的 'using' 语句。
希望对您有所帮助。