我正在尝试创建可以将图像复制到文件夹中的图像上传。问题是,当我尝试将照片复制到文件夹时,会出现一个错误,表示无法找到该文件。我的代码如下:
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;
}
}
答案 0 :(得分:1)
我认为您正在丢失源文件上的完整文件路径。
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);
关于您的其他问题,请参阅评论:
&#34;即使我用jpeg设置了文件管理器,我的jpeg的1张图片怎么会触发我的错误信息?&#34;
这很可能是一个区分大小写的问题。您的代码只允许使用小写文件扩展名;如果您选择的文件是&#34; .JPEG&#34; (全部大写)它将失败并显示错误消息。
您可以通过调用接受StringComparison
类型的重载方法来忽略大小写:
int index = Required_Ext.IndexOf(
selected_ext, StringComparison.CurrentCultureIgnoreCase);