c# - C#如何复制保存在变量中的文件并将其粘贴到不同的位置?
问题描述
目标:
我正在尝试复制存储在变量中的文件..,然后将其粘贴到指定的另一个文件夹中。
我看过这里:https ://stackoverflow.com/a/53731042/12129150但这没有帮助,因为我在变量中有一条路径。
这是我尝试过的:
// File path attached
private string filePath = null;
private void button2_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "All Files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
File.Copy(filePath, @"C:\Users\user\Desktop\@filePath");
}
但这只是将其保存到桌面@filePath
解决方案
自行获取文件名
private void button1_Click(object sender, EventArgs e)
{
var fileName = Path.GetFileName(filePath);
var newPath = Path.Combine(@"C:\Users\user\Desktop", fileName);
File.Copy(filePath, newPath);
}
推荐阅读
- html - HTML 报告的标题默认为报告的底部
- python-3.x - 如果字符串包含另一个字符串 Python 的元素列表中的单词,则提取部分字符串
- python - 从远离 main() 的文件中导入 Python 模块
- python - 如何让用户在图像上设置 4 个点?
- node.js - 如何使用节点 sdk 在代码中为 azure 函数生成 openAPI 规范?
- r - 用 plotly 绘制圆线段
- java - 如何从 Intellij 项目中获取 .java 文件?
- python - 按颜色自定义悬停散点图
- acumatica - 无法将值设置为 INTran.LotSerialNbr 或 INTran.ExpireDate
- c# - EF 6 Database.Migrate 不适用或在链接项目中看到新迁移