我正在尝试构建一个复印机,以便您使用 openFileDialog 选择一个文件,然后使用 folderBrowserDialog 选择要将其复制到的位置。
我遇到的问题是,当我使用 File.Copy(copyFrom,copyTo) 时,它给了我一个无法复制到目录的异常。
无论如何,这是否存在,或者我只是错过了一些愚蠢和愚蠢的东西。我已经尝试使用 openFD 来选择这两个位置,并且刚刚尝试使用 folderBD 来查看它是否有所作为。
我知道那里应该有 if 语句来捕获异常,但这是开始工作的代码的粗略草案。
提前感谢您的帮助,附上代码。
// Declare for use in all methods
public string copyFrom;
public string copyTo;
public string rootFolder = @"C:\Documents and Settings\cmolloy\My Documents";
private void btnCopyFrom_Click(object sender, EventArgs e)
{
// uses a openFileDialog, openFD, to chose the file to copy
copyFrom = "";
openFD.InitialDirectory = rootFolder;
openFD.FileName = "";
openFD.ShowDialog();
// sets copyFrom = to the file chosen from the openFD
copyFrom = openFD.FileName;
// shows it in a textbox
txtCopyFrom.Text = copyFrom;
}
private void btnCopyTo_Click(object sender, EventArgs e)
{
//uses folderBrowserDialog, folderBD, to chose the folder to copy to
copyTo = "";
this.folderBD.RootFolder = System.Environment.SpecialFolder.MyDocuments;
this.folderBD.ShowNewFolderButton = false;
folderBD.ShowDialog();
DialogResult result = this.folderBD.ShowDialog();
// sets copyTo = to the folder chosen from folderBD
copyTo = this.folderBD.SelectedPath;
//shows it in a textbox.
txtCopyTo.Text = copyTo;
}
private void btnCopy_Click(object sender, EventArgs e)
{
// copys file
File.Copy(copyFrom, copyTo);
MessageBox.Show("File Copied");