I am looking for a way to move a file from one directory to another using C#. I have a forms application that I would like to have the user select a file using the file chooser and upon clicking the "Set Background" button have the file transferred to a location specified within the application.
我正在寻找一种方法,使用C#将文件从一个目录移动到另一个目录。我有一个表单应用程序,我希望用户使用文件选择器选择一个文件,并在单击“设置背景”按钮时将文件传输到应用程序中指定的位置。
Upon trying the answer provided by @VulgarBinary I am getting the following exception:
在尝试@VulgarBinary提供的答案后,我得到以下异常:
System.IO.IOException: Cannot create a file when that file already exists.
2 个解决方案
#1
You will need to ensure your program has appropriate permissions to write the files but:
您需要确保您的程序具有编写文件的适当权限,但是:
if (File.Exists(sourcePath))
{
File.Move(sourcePath, destinationPath);
}
This should work to do what you are wanting to do.
这应该可以做你想做的事情。
Example:
var sourcePath = "C:\Users\someuser\Pictures\VulgarBinary.jpg";
var destinationPath = "C:\Whatever\Path\You\Want\VulgarBinary.jpg";
EDIT 1
Given your comments below this answer you are running into a problem where the file you are creating already exists. If you would like to replace this you can simple do:
鉴于您在此答案下方的评论,您遇到的问题是您正在创建的文件已存在。如果你想替换它,你可以简单地做:
if (File.Exists(sourcePath))
{
if(File.Exists(destinationPath))
File.Delete(destinationPath);
File.Move(sourcePath, destinationPath);
}
If you don't care what the output file's name is and just always want to write it you can do something like:
如果您不关心输出文件的名称是什么,并且总是想要写它,您可以执行以下操作:
var outputDirectory = "C:\\Whatever\\Path\\You\\Want\\";
if (File.Exists(sourcePath))
{
File.Move(sourcePath, outputDirectory + Guid.NewGuid().ToString() + ".jpg");
}
The latter will always copy the file (all-be-it with a different name). The first solution will replace any file with the same name with your new file.
后者将始终复制文件(所有人都使用不同的名称)。第一个解决方案将使用新文件替换任何具有相同名称的文件。
Cheers!
#2
Here is a code sample and scaffold for you in which I use @VulgarBinary´s proposal.
这是一个代码示例和脚手架,我使用@ VulgarBinary的提议。
private string sourcePath;
private string destinationPath;
public Form1()
{
destinationPath = @"c:\users\name\desktop\"; // f.e.
InitializeComponent();
}
//Browse Button
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp"; // you can filter whatever format you want
if (dlg.ShowDialog() == DialogResult.OK)
{
sourcePath = dlg.FileName;
}
}
}
//Set Background Button
private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(sourcePath) && File.Exists(sourcePath))
{
destinationPath += Path.GetFileName(sourcePath);
File.Move(sourcePath, destinationPath);
}
}
#1
You will need to ensure your program has appropriate permissions to write the files but:
您需要确保您的程序具有编写文件的适当权限,但是:
if (File.Exists(sourcePath))
{
File.Move(sourcePath, destinationPath);
}
This should work to do what you are wanting to do.
这应该可以做你想做的事情。
Example:
var sourcePath = "C:\Users\someuser\Pictures\VulgarBinary.jpg";
var destinationPath = "C:\Whatever\Path\You\Want\VulgarBinary.jpg";
EDIT 1
Given your comments below this answer you are running into a problem where the file you are creating already exists. If you would like to replace this you can simple do:
鉴于您在此答案下方的评论,您遇到的问题是您正在创建的文件已存在。如果你想替换它,你可以简单地做:
if (File.Exists(sourcePath))
{
if(File.Exists(destinationPath))
File.Delete(destinationPath);
File.Move(sourcePath, destinationPath);
}
If you don't care what the output file's name is and just always want to write it you can do something like:
如果您不关心输出文件的名称是什么,并且总是想要写它,您可以执行以下操作:
var outputDirectory = "C:\\Whatever\\Path\\You\\Want\\";
if (File.Exists(sourcePath))
{
File.Move(sourcePath, outputDirectory + Guid.NewGuid().ToString() + ".jpg");
}
The latter will always copy the file (all-be-it with a different name). The first solution will replace any file with the same name with your new file.
后者将始终复制文件(所有人都使用不同的名称)。第一个解决方案将使用新文件替换任何具有相同名称的文件。
Cheers!
#2
Here is a code sample and scaffold for you in which I use @VulgarBinary´s proposal.
这是一个代码示例和脚手架,我使用@ VulgarBinary的提议。
private string sourcePath;
private string destinationPath;
public Form1()
{
destinationPath = @"c:\users\name\desktop\"; // f.e.
InitializeComponent();
}
//Browse Button
private void button1_Click(object sender, EventArgs e)
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Open Image";
dlg.Filter = "bmp files (*.bmp)|*.bmp"; // you can filter whatever format you want
if (dlg.ShowDialog() == DialogResult.OK)
{
sourcePath = dlg.FileName;
}
}
}
//Set Background Button
private void button2_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(sourcePath) && File.Exists(sourcePath))
{
destinationPath += Path.GetFileName(sourcePath);
File.Move(sourcePath, destinationPath);
}
}