如何在WPF中使用C#将List >存储到文本文件中?(How can i store my List> to a text file using C# in WPF?)
- > to a text file using C# in WPF?)
我正在尝试使用C#将
List<List<Class>>
到文本文件中,我的类就像public class cEspecie { private string nombre; private BitmapImage imagen; private int tiempodevida; private int movilidad; private int decaimiento; private int tipo; private string colordelagrafica; private bool seleccion; public int Tipo { get { return tipo; } set { tipo = value; } } public string Nombre { get { return nombre; } set { nombre = value; } } public BitmapImage Imagen { get { return imagen; } set { imagen = value; } } public int TiempoDeVida { get { return tiempodevida; } set { tiempodevida = value; } } public int Movilidad { get { return movilidad; } set { movilidad = value; } } public int Decaimiento { get { return decaimiento; } set { decaimiento = value; } } public string ColorDeLaGrafica { get { return colordelagrafica; } set { colordelagrafica = value; } } public bool Seleccion { get { return seleccion; } set { seleccion = value; } } }
List<List<cEspecie>> items1 = new List<List<cEspecie>>();
如何从“items1”中获取所有数据,以便使用C#将其存储在文本文件中? 以及如何将其存回以便将其存储在另一个列表中?
i'm trying to store a
List<List<Class>>
to a text file using C#, my class is likepublic class cEspecie { private string nombre; private BitmapImage imagen; private int tiempodevida; private int movilidad; private int decaimiento; private int tipo; private string colordelagrafica; private bool seleccion; public int Tipo { get { return tipo; } set { tipo = value; } } public string Nombre { get { return nombre; } set { nombre = value; } } public BitmapImage Imagen { get { return imagen; } set { imagen = value; } } public int TiempoDeVida { get { return tiempodevida; } set { tiempodevida = value; } } public int Movilidad { get { return movilidad; } set { movilidad = value; } } public int Decaimiento { get { return decaimiento; } set { decaimiento = value; } } public string ColorDeLaGrafica { get { return colordelagrafica; } set { colordelagrafica = value; } } public bool Seleccion { get { return seleccion; } set { seleccion = value; } } }
List<List<cEspecie>> items1 = new List<List<cEspecie>>();
How can I take all my data from "items1" in order to store it inside a Text file using C#? and also how can I make it back in order to store it in another list?
原文:https://stackoverflow.com/questions/19918025
最满意答案
您可以使用
Directory.GetFiles
重载和SearchOption.AllDirectories
选项枚举目录include子目录中的所有文件,然后使用它String[] allfiles = System.IO.Directory.GetFiles("myPath", "*.*", System.IO.SearchOption.AllDirectories); foreach (string file in allfiles) { newFile.CreateEntryFromFile(file); }
您可以使用
ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName)
或其重载来一步创建存档。还有一种方法可以通过
Path.GetDirectoryName()
方法更灵活地操作zip归档结构。 这是一个例子:foreach(var filePath in files) { var directory = Path.GetDirectoryName(filePath); var fileName = Path.GetFileName(filePath); var entry = archive.GetEntryFromFile(filePath, $"{directory}/{fileName}"); }
最后,您可以使用第三方库DotNetZip并在3行代码中解决您的场景:
using (ZipFile zip = new ZipFile()) { zip.AddFiles(files); zip.Save("Archive.zip"); }
You can enumerate all files in the directory include subdirectories with
Directory.GetFiles
overloading andSearchOption.AllDirectories
option and then use itString[] allfiles = System.IO.Directory.GetFiles("myPath", "*.*", System.IO.SearchOption.AllDirectories); foreach (string file in allfiles) { newFile.CreateEntryFromFile(file); }
You can use
ZipFile.CreateFromDirectory(string sourceDirectoryName, string destinationArchiveFileName)
or its overload to create an archive in one step too.Also there is a way to manipulate zip archive structure more flexibly via
Path.GetDirectoryName()
method. Here is the example:foreach(var filePath in files) { var directory = Path.GetDirectoryName(filePath); var fileName = Path.GetFileName(filePath); var entry = archive.GetEntryFromFile(filePath, $"{directory}/{fileName}"); }
Finally, you can use 3rd party library DotNetZip and solve yor scenario in just 3 lines of code:
using (ZipFile zip = new ZipFile()) { zip.AddFiles(files); zip.Save("Archive.zip"); }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
在资源中添加此zip文件(项目属性/资源/添加资源/添加现有文件),并使用SharpZipLib从zip中提取它,假设您的zip文件是 scripts.zip 用于提取字符串中脚本的代码: ZipInputStream zip = new ZipInputStream(new MemoryStream(Properties.Resources.scripts)); while (zip.GetNextEntry() != null) { MemoryStream data = new MemoryStr ...
-
您可以使用Directory.GetFiles重载和SearchOption.AllDirectories选项枚举目录include子目录中的所有文件,然后使用它 String[] allfiles = System.IO.Directory.GetFiles("myPath", "*.*", System.IO.SearchOption.AllDirectories); foreach (string file in allfiles) { newFile.CreateEntryFromFile( ...
-
尝试使用SharpZibLib压缩Liam Westley的文章 http://geekswithblogs.net/twickers/archive/2005/11/08/59420.aspx try checking out Liam Westley's article on zipping with SharpZibLib http://geekswithblogs.net/twickers/archive/2005/11/08/59420.aspx
-
您可以在ZipArchive上尝试foreach循环,并排除与您的参数不匹配的所有内容,然后,在循环完成后,提取最后一个文件。 像这样的东西: private void TestUnzip_Foreach() { using (ZipArchive z = ZipFile.Open("zipfile.zip", ZipArchiveMode.Read)) { string LastFile = "lastFileName.ext"; ...
-
我最终使用了两个不同的zip库。 DotNetZip的ZipOutputStream允许我指定mimefile是第一个和未压缩的。 然后我使用它的ZipFile类添加必要的文件夹及其内容。 我在使用DotNetZip的Extract()方法时遇到了麻烦,所以我实际上是使用.net的压缩库来解压缩文件而使用DotNetZip将其压缩回来。 我仍在寻找一种更好的方法来做到这一点,但它现在有效。 I ended up using two different zip libraries. DotNetZip's Z ...
-
您应该使用7zip SDK或SevenZipSharp 。 要回答您的问题,请将RedirectStandardOutput设置为true,然后读取t.Output 。 You should use the 7zip SDK or SevenZipSharp. To answer your question, set RedirectStandardOutput to true, then read t.Output.
-
你写了一段打开文件并写入的代码。 一旦创建文件并在其中写入,您应该告诉PC关闭文件。 我不完全确定这个例子中的命令,尝试类似的东西 zipFile.close; you have written a piece of code that opens the file and writes. Once the process of creating the file and writing in it, you should tell the PC to close the file. Im not exa ...
-
使用Microsoft.Deployment.Compression.Zip的c#zip文件(c# zip files using Microsoft.Deployment.Compression.Zip)[2022-06-19]
试试这个 : http://dotnetzip.codeplex.com/ 示例代码: using (ZipFile zip = new ZipFile()) { // add this map file into the "images" directory in the zip archive zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images"); // add the report into a d ...