我正在使用System.Windows.Forms.OpenFileDialog
我的WPF应用程序来选择图像.当用户选择图像时,我在文本框中显示所选文件的文件名,如下所示.
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Select image";
fileDialog.InitialDirectory = "";
fileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png";
fileDialog.FilterIndex = 1;
fileDialog.RestoreDirectory = true;
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
txtImagePath.Text = fileDialog.FileName;
}
Run Code Online (Sandbox Code Playgroud)
Save
我的应用程序中有一个按钮.当用户单击此按钮时,我需要将此文件重命名为其他名称并将其复制到我的硬盘驱动器中的另一个目录.
我怎样才能实现这一目标?
使用Path类中的File.Copy和方法来提取文件的相关部分
string newDir = @"D:\temp";
string curFile = Path.GetFileName(txtImagePath.Text);
string newPathToFile = Path.Combine(newDir, curFile);
File.Copy(txtImagePath.Text, newPathToFile);
Run Code Online (Sandbox Code Playgroud)
现在使用File.Move重命名当前目录上的操作
string curDir = Path.GetDirectoryName(textImagePath.Text);
File.Move(txtImagePath.Text, Path.Combine(curDir, "NewNameForFile.txt"));
Run Code Online (Sandbox Code Playgroud)
可以改进此代码,引入一些错误处理
如果要使用新名称直接复制新目录中的旧文件,则可以简单地编写
string newPathToFile = @"D:\temp\NewNameForFile.txt";
File.Copy(txtImagePath.Text, newPathToFile);
Run Code Online (Sandbox Code Playgroud)
然后在当前目录上重命名.
归档时间: |
|
查看次数: |
9182 次 |
最近记录: |