![](/img/trans.png)
[英]How to export properties of objects in a list to CSV efficiently in C#?
[英]C# How to build list of objects and change their properties
我只是针对一个更基本的情况取消了前面的整个问题:我想制作一个简单的load + display程序,例如当我加载文本文件时,文件名,文件路径和内容显示在3个不同的文本框中。
这是代码:
using System.IO;
using System.Windows.Forms;
namespace ListBuilderTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public class TextFile
{
public string TextFilePath {get;set;}
public string TextFileTitle {get;set;}
public string TextFileString {get;set;}
}
List<TextFile> TextFileList = new List<TextFile>();
string pathToFile = "";
private void button_Click(object sender, RoutedEventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text files|*.txt", ValidateNames = true, Multiselect = true })
{
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (String file in ofd.FileNames)
{
TextFile CurrentFile = new TextFile();
CurrentFile.TextFilePath = ofd.SafeFileName;
CurrentFile.TextFileTitle = ofd.FileName;
if (File.Exists(pathToFile)) {
using (StreamReader sr = new StreamReader(pathToFile))
{
CurrentFile.TextFileString = sr.ReadLine();
}
}
TextFileList.Add(CurrentFile);
/*
textBox.Text = CurrentFile.TextFileTitle+"\r\n";
textBox_Copy.Text = CurrentFile.TextFilePath+"\r\n";
textBox_Copy1.Text = CurrentFile.TextFileString+"\r\n";*/
/* I would like to simplify this preceeding code into displaying it as a reference to the list rather than the current object.*/
}
}
}
}
}
}
OFD可以使用,但是我可以选择多个文件,但是它只会获取第一个文件的属性。
我的文件是Apple.txt,Banana.txt和Cherry.txt,它们分别包含“红色”,“黄色”和“黑色”。
您将在列表中添加如下内容:
PDFList.Add(new PDFfile { PDFFileName = "name", PDFText = "text" });
您的foreach周期应如下所示:
foreach (PDFfile file in PDFList){
if(Regex.IsMatch(file.PDFText, "statement") == true)
{
file.PDFresult1 = "true";
}
else
{
file.PDFresult1 = "false";
}
}
其余的应该没问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.