按照列表C#的顺序转换文件

问题描述 投票:0回答:1

我需要将图像(如.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));
    }
c# arrays .net list converters
1个回答
0
投票

如果两个动作都发生在同一个进程中,您只需将文件名列表存储在内存中(并且已经将它们添加到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列表框以使订单正确。

© www.soinside.com 2019 - 2024. All rights reserved.