[英]C# saving mulitple filenames from OpenFileDialog
I have a form with an add button. 我有一个带有添加按钮的表单。 When clicked, a user selects a file or files from the dialog.
单击时,用户从对话框中选择一个或多个文件。
My Goal: 我的目标:
Retrieve the names of all the files that a user selects (from whichever directory their file(s) are in) , copy those files in a specified folder that the user doesn't choose using File.Copy (I hard-code a filepath and filename). 检索用户选择的所有文件的名称(从文件所在的目录),使用File.Copy将这些文件复制到用户未选择的指定文件夹中(我对文件路径和文档名称)。
My Issue: 我的问题:
If the user only selects one, this works fine. 如果用户仅选择一个,则效果很好。 For example:
例如:
string name = System.IO.Path.GetFileName(sfd.FileName)
; string name = System.IO.Path.GetFileName(sfd.FileName)
;
This grabs the file. 这将抓取文件。 Then:
然后:
DialogResult dialogResult = MessageBox.Show("Is this published?", "", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published");
File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\Published\\" + name);
}
else if (dialogResult == DialogResult.No)
{
Directory.CreateDirectory("c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished");
File.Copy(sfd.FileName, "c:\\NewTest\\" + txtAcronym.Text + "\\" + txtMajor.Text + "." + txtMinor.Text + "\\NonPublished\\" + name);
}
I ask the user if the document is published. 我问用户文件是否出版。 Based on the answer, it will create a directory and put the file in that directory.
根据答案,它将创建一个目录并将文件放在该目录中。
Is it possible to loop through multiple filenames in the openFileDialog and put them all in a folder , rather than just one? 是否可以在openFileDialog中循环遍历多个文件名并将它们全部放入一个文件夹中,而不是一个文件夹中?
Set the Multiselect to true. 将Multiselect设置为true。
myFileDialog.Multiselect = true;
When the user accepts the selection, you can get them with FileNames
property. 当用户接受选择时,可以使用
FileNames
属性获得它们。 It returns a string[] . 它返回一个string [] 。 Note the difference with
FileName
which returns string . 注意与
FileName
的区别,后者返回string 。 You can use a for or foreach to get all the results. 您可以使用for或foreach获得所有结果。
foreach (string file in myFileDialog.FileNames)
{
//do work
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.