c# 用OpenFileDialog打开的文件复制到别的目录上
3个回答
展开全部
//先获取文件路径
try
{
openFileDialog1.Multiselect=true; //可以选择多个文件
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
foreach(string file in openFileDialog1.FileNames)
{
textBox4.Text = Path.GetFileName(file);//获取文件名
//name=fileName;
this.textBox2.Text = openFileDialog1.FileName;//获取文件名以及路径
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
------------------------
获取源文件路径和复制保存的文件路径,关于保存多个,可能要用到数组.
if(textBox2.Text==""||textBox3.Text=="")
{
MessageBox.Show("无效路径");
}
else
{
File.Copy(textBox2.Text,textBox3.Text);
MessageBox.Show("复制成功");
}
-------------
这个比较合适初学者用.
try
{
openFileDialog1.Multiselect=true; //可以选择多个文件
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
foreach(string file in openFileDialog1.FileNames)
{
textBox4.Text = Path.GetFileName(file);//获取文件名
//name=fileName;
this.textBox2.Text = openFileDialog1.FileName;//获取文件名以及路径
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
------------------------
获取源文件路径和复制保存的文件路径,关于保存多个,可能要用到数组.
if(textBox2.Text==""||textBox3.Text=="")
{
MessageBox.Show("无效路径");
}
else
{
File.Copy(textBox2.Text,textBox3.Text);
MessageBox.Show("复制成功");
}
-------------
这个比较合适初学者用.
展开全部
这个我以前做过的:
private void 拷贝复制IToolStripMenuItem_Click(object sender, EventArgs e)
{
string DestDir = @"D:\123"; //假设目录已经存在
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "All files (*.*)|*.*";
if(DialogResult.OK == openFileDialog.ShowDialog())
{
File.Copy(openFileDialog.FileName, DestDir + "\\" + Path.GetFileName);
}
}
private void 拷贝复制IToolStripMenuItem_Click(object sender, EventArgs e)
{
string DestDir = @"D:\123"; //假设目录已经存在
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "All files (*.*)|*.*";
if(DialogResult.OK == openFileDialog.ShowDialog())
{
File.Copy(openFileDialog.FileName, DestDir + "\\" + Path.GetFileName);
}
}
参考资料: 我写的,呵呵
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//在fileOk事件中如下
//另外,我这里是没有对d:\123目录作判断的,所以要是没这个目录会出错
//要加上判断也是很简单的事,你自己可以看一下System.IO的东东
using System.IO;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (openFileDialog1.SafeFileNames.Length <= 0) return;
string dir = openFileDialog1.FileNames[0].Replace(openFileDialog1.SafeFileNames[0],"");
foreach (string item in openFileDialog1.SafeFileNames)
{
File.Copy(dir + item, @"D:\123" + item);
}
}
//另外,我这里是没有对d:\123目录作判断的,所以要是没这个目录会出错
//要加上判断也是很简单的事,你自己可以看一下System.IO的东东
using System.IO;
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
if (openFileDialog1.SafeFileNames.Length <= 0) return;
string dir = openFileDialog1.FileNames[0].Replace(openFileDialog1.SafeFileNames[0],"");
foreach (string item in openFileDialog1.SafeFileNames)
{
File.Copy(dir + item, @"D:\123" + item);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询