[英]How do I use FileDialog to create a directory, then automatically copy files to new directory?
In my program, I would like to use a FileDialog to access audio files, then automatically copy the selected file(s) to a new folder upon clicking "open".在我的程序中,我想使用 FileDialog 访问音频文件,然后在单击“打开”时自动将所选文件复制到新文件夹中。 I want to do this so I can make changes to a sample without changing the original file.
我想这样做,这样我就可以在不更改原始文件的情况下对示例进行更改。
My problem is that whenever I select a file then click "open" in the file dialog, the program crashes or closes.我的问题是,每当我选择一个文件然后在文件对话框中单击“打开”时,程序就会崩溃或关闭。
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Wave File (*.wav)|*.wav;";
if (open.ShowDialog() == true)
{
bool directoryExists = Directory.Exists(@"C:\Documents\LUX\Soundbites\");
if (directoryExists == true)
{
string[] files = open.FileNames;
foreach (string file in files)
{
var info = new FileInfo(file);
File.Copy(file, @"C:\Documents\LUX\Soundbites\", true);
}
}
if (directoryExists != true)
{
Directory.CreateDirectory(@"C:\Documents\LUX\");
Directory.CreateDirectory(@"C:\Documents\LUX\Soundbites\");
string[] files = open.FileNames;
foreach (string file in files)
{
var info = new FileInfo(file);
File.Copy(file, @"C:\Documents\LUX\Soundbites\", true);
}
}
}
I'm sure this is a common operation, but I'm new to developing and I haven't found any real answers online.我确信这是一个常见的操作,但我是开发新手,我还没有在网上找到任何真正的答案。
File.Copy的第二个参数需要您在File.Copy代码中将文件复制到的文件名,而不是目录,将其更改为:
File.Copy(file, @"C:\Documents\LUX\Soundbites\" + System.IO.Path.GetFileName(file), true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.