找不到文件的System.IO.File.Copy错误
本文关键字:File Copy 错误 IO System 文件 找不到 | 更新日期: 2023-09-27 17:59:22
我正在尝试创建一个可以将图像复制到文件夹中的图像上传。问题是,当我试图将照片复制到文件夹时,出现了一个错误,表明它找不到文件。我的代码如下:
private void btn_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
if (result == DialogResult.Cancel)
{
return;
}
//001: Check the user selected a file and pressed ok button. Before opening the dialog
// set the filters
dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
string path = folderBrowserDialog1.SelectedPath;
if (dlgFileOpen.ShowDialog() == DialogResult.OK)
{
// string file = Path.GetFileName(dlgFileOpen.FileName);
string file = Path.GetFileName(dlgFileOpen.FileName);
//string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
string newFileName = System.IO.Path.Combine(path, file);
System.IO.File.Copy(file, newFileName, true);
//image directory to be saved in database
//txtSelectedFile.Text = newFileName;
}
}
private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
{
string Required_Ext = ".jpeg .jpg .bmp";
string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
int index = Required_Ext.IndexOf(selected_ext);
//002: Inform the user to select correct extension
if (index < 0)
{
MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
e.Cancel = true;
}
}
答案:
private void btn_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// We print the number of files found.
//
string [] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
}
if (result == DialogResult.Cancel)
{
return;
}
//001: Check the user selected a file and pressed ok button. Before opening the dialog
// set the filters
dlgFileOpen.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
string path = folderBrowserDialog1.SelectedPath;
if (dlgFileOpen.ShowDialog() == DialogResult.OK)
{
// string file = Path.GetFileName(dlgFileOpen.FileName);
string file = Path.GetFileName(dlgFileOpen.FileName);
//string file2 = System.IO.Path.Combine(file, Path.GetFileName(dlgFileOpen.FileName));
string newFileName = System.IO.Path.Combine(path, file);
System.IO.File.Copy(dlgFileOpen.FileName, newFileName, true);
txtSelectedFile.Text = newFileName;
MessageBox.Show("Image uploaded successful!");
}
}
private void dlgFileOpen_FileOk(object sender, CancelEventArgs e)
{
string Required_Ext = ".jpeg .jpg .bmp";
string selected_ext = Path.GetExtension(dlgFileOpen.FileName);
int index = Required_Ext.IndexOf(selected_ext, StringComparison.CurrentCultureIgnoreCase);
//002: Inform the user to select correct extension
if (index < 0)
{
MessageBox.Show("Extension Maaaan... Extension! Open only jpeg or bmp or jpg");
e.Cancel = true;
}
}
我认为您正在丢失源文件上的完整文件路径。
string file = Path.GetFileName(dlgFileOpen.FileName); // "file" is the file name
string newFileName = System.IO.Path.Combine(path, file);
System.IO.File.Copy(file, newFileName, true);
替换此:
System.IO.File.Copy(file, newFileName, true);
有了这个:
System.IO.File.Copy(dlgFileOpen.FileName, newFileName, true);
关于你的另一个问题,来自评论:
"即使我用jpeg设置了文件管理器,为什么我的一张jpeg图片会触发我的错误消息?"
这很可能是一个案件敏感性问题。您的代码只允许使用小写文件扩展名;如果您选择的文件是".JPEG"(全部大写),它将失败并显示错误消息。
您可以通过调用接受StringComparison
类型的重载方法来忽略case:
int index = Required_Ext.IndexOf(
selected_ext, StringComparison.CurrentCultureIgnoreCase);