【问题标题】:Convert files following the order of a list C#按照列表 C# 的顺序转换文件
【发布时间】:2019-07-16 21:01:17
【问题描述】:

我需要将图像(如 .jpg)转换为 PDF 文件以用于学校作业。我有一个ListBox,我在其中放置了 PDF 文件的页面,因此用户可以重新排序列表并按该顺序转换文件。 我有一个临时文件夹中的文件,以便将文件转换为 PDF。

我的问题是:如何按照用户选择的顺序转换文件?

我已经搜索过,并尝试使用字符串 IDName 进行类,因此我从 ListBox 中的项目中获取 ID 并将其更改为新列表。我想之后,我做了一个 foreach() 循环,我从临时文件夹中获取文件并将它们合并到一个新的 PDF 文件中,但是按照我想要的顺序,我想我必须比较文件的名称使用列表中的名称,如果匹配,则转换并添加它,如果不匹配,则传递到下一个文件。 但我不知道该怎么做。

请有人帮我解决这个问题吗? 提前致谢!

我将我的代码发送到:

   //the open files button
   private void proc2_Click(object sender, EventArgs e)
   {
        OpenFileDialog dialogo = new OpenFileDialog();

        dialogo.Title = "Search files";

        dialogo.InitialDirectory = @"E:\";

        dialogo.Filter = "Images (.bmp,.jpg,.png,.tiff,.tif) |*.bmp;*.jpg;*.png;*tiff;*tif|All of the files (*.*)|*.*";

        DialogResult resposta = dialogo.ShowDialog();
        if (resposta == DialogResult.OK)
        {
            string caminhoCompleto = dialogo.FileName;
            caminho2 = dialogo.SafeFileName;
            caminhotb2.Text = caminhoCompleto;
            string fish = "";
            string path = @"C:\temporario";
            if(Directory.Exists(path))
            {
                 fish=Path.Combine(path, caminho2);                   
            }
            else
            {
                Directory.CreateDirectory(path);
                fish = Path.Combine(path, caminho2);
            }
            File.Create(fish);
            listaimg.Items.Add(caminho2);
        }
    }

    public string[] GetFilesImg4() //jpg files
    {
        if (!Directory.Exists(@"C:\temporario"))
        {
            Directory.CreateDirectory(@"C:\temporario");
        }
        DirectoryInfo dirInfo = new DirectoryInfo(@"C:\temporario");
        FileInfo[] fileInfos4 = dirInfo.GetFiles("*.jpg");
        foreach (FileInfo info in fileInfos4)
        {
            if (info.Name.IndexOf("protected") == -1)
                list4.Add(info.FullName);
        }

        return (string[])list4.ToArray(typeof(string));
    }

【问题讨论】:

  • 提示:更改临时文件夹中的文件名以包含选择的订单号。或者用选定的编号创建一堆临时文件夹。最好的方法是创建一个 List> ,其中 int 是选择顺序,字符串是所选文件的完整路径名。
  • 为什么不只迭代ListBox 中的项目(这将为您提供正确的顺序)?
  • 为什么顺序很重要?文件的选择和文件列表的获取是在同一个进程中发生还是在不同进程中发生?
  • 谢谢大家。订单对我来说很重要,因为我想在我的项目中添加一些东西,这样对用户来说更完整@aiodintsov
  • @Zer0 我无法理解你的评论,你能说得更清楚些吗?

标签: c# arrays .net list converters


【解决方案1】:

如果这两个操作发生在同一个进程中,您可以将文件名列表存储在内存中(并且您已经将它们添加到 listaimg):

public string[] GetFilesImg4() //jpg files
{
    string tempPath = @"C:\temporario";
    if (!Directory.Exists(tempPath))
    {
        foreach (string filename in listimga.Items)
        {
            if (!filename.Contains("protected"))
                list4.Add(Path.Combine(tempPath, filename);
        }
    }

    return (string[])list4.ToArray(typeof(string));
}

如果这些是不同的进程,那么您可以在某个时候转储 listimga 的内容,然后从同一个文件中读取它。在下面的例子中,我将其存储到同一目录下名为“order.txt”的文件中,但逻辑可能更复杂,例如合并多个带有时间戳的文件等。

// somewhere in after selecting all files
File.WriteAllLines(@"c:\temporario\order.txt", listimga.Items.Select(t=>t.ToString()));


public string[] GetFilesImg4() //jpg files
{
    string tempPath = @"C:\temporario";
    if (!Directory.Exists(tempPath))
    {
        var orderedFilenames = File.ReadAllLines(Path.Combine(tempPath, "order.txt")); // list of files loaded in order
        foreach (string filename in orderedFilenames)
        {
            if (!filename.Contains("protected"))
                list4.Add(Path.Combine(tempPath, filename);
        }
    }

    return (string[])list4.ToArray(typeof(string));
}

检查类的可用方法也是一个好主意,例如在这种情况下string.IndexOf(s) == -1 等同于!string.Contains(s),至少对于说英语的人来说,后者更具可读性。

我还注意到您的用户必须逐个选择文档,但 FileOpen 对话框允许一次选择多个文件,我相信它也保留了选择顺序。

如果选择顺序很重要并且文件打开对话框不保留顺序或用户发现很难遵循您仍然可以使用多个文件选择打开对话框然后允许重新排列您的listimga 列表框以获得正确的顺序.

【讨论】:

  • 我正在尝试,如果有效,我将其标记为答案,提前谢谢。
  • 我相信您在listaimg.Items.Select() 中有错误,因为List.Items 中没有这样的“Select()”
  • @SofiaRodrigues 那是 LINQ。确保添加using System.Linq;
  • @SofiaRodrigues 正如 Zer0 提到的,它是 LINQ。这是使用集合的便捷方式。检查docs.microsoft.com/en-us/dotnet/api/…和那里的其他方法,它们经常使用。
  • 那么,让我看看我是否正确...我应该使用 Linq 进行查询。例如,我必须将第二个代码的第一行放在转换按钮中,但在我调用转换函数之前。然后其余的代码将获取该文件的每一行都将按该顺序....是这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-21
相关资源
最近更新 更多