1

我的应用程序具有读取文件名并在 DataGridView 的第一列中逐个显示它们的功能。我想要做的是,如果用户在其原始名称旁边的第二列中输入新名称,然后按“另存为”按钮,则文件将根据列表的顺序保存为新输入的名称.

我没有将它与 DB 或类似的东西绑定。

我的猜测是,如果用户单击另存为按钮,它会调用每个新命名块的代码,并将该代码带入“另存为”功能。但我不知道我怎么能意识到这一点。也许我的猜测完全错误;您可以提供一些建议,以便我找到正确的方法!

我恳请您帮忙!

这是我的代码

 private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
            ofd.Multiselect = true;
            string ndn = "";//neue Dateinamen
            bool umlaut, pdf, wasserzeichen;
            int kopien;
            

            
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {

                    string[] selectedFiles = ofd.SafeFileNames;
                    for (int i = 0; i < ofd.FileNames.Count() - 1; i++)
                    {
                        dataGridView1.Rows.Add(selectedFiles[i]);
                        dataGridView1.Rows[i].Cells["Dateinamen"].Value.ToString();
                        dataGridView1.Rows[i].Cells["Neue Dateinamen"].Value.ToString();  // Here I tried to save values of new names
                        dataGridView1.Rows[i].Cells["Kopien"].Value.ToString();
                        dataGridView1.Rows[i].Cells["Wasserzeichen"].Value.ToString();
                        dataGridView1.Rows[i].Cells["Umlaut"].Value.ToString();
                        dataGridView1.Rows[i].Cells["PDF"].Value.ToString();

      
                    }
                }
          
 private void button7_Click(object sender, EventArgs e) //Save as
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Word(*.docx)| *.docx|PPT(*.pptx)|*.pptx|PDF(*.pdf)|*.pdf|Alle Dateien(*.*)|*.*";
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.CheckFileExists = true;
            saveFileDialog1.CheckPathExists = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {

                }

            }
        }

        }
4

1 回答 1

0

为保存按钮尝试以下代码

private void SaveWithName_Click(object sender, EventArgs e)
    {
        int row = dataGridView1.CurrentCell.RowIndex;
        string OldName = Path.Combine(FilesPath, dataGridView1.Rows[row].Cells[0].Value.ToString());
        string NewName = Path.Combine(FilesPath, dataGridView1.Rows[row].Cells[1].Value.ToString()) ;
        System.IO.File.Copy(OldName, NewName);
    }

变量 FilesPath 是一个公共字符串。此变量将包含您选择的文件所在的目录名称。看一个读取文件列表的按钮的代码示例

if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string[] selectedFiles = ofd.SafeFileNames;
            FilesPath = Path.GetDirectoryName(ofd.FileName);

            label1.Text = FilesPath;
            for (int i = 0; i < ofd.FileNames.Count(); i++)
            {
                dataGridView1.Rows.Add(selectedFiles[i]);
            }
        }

在那里,您可以看到填充了变量 FilesPath 的行。

于 2022-02-10T13:31:16.777 回答