Reputation: 1
I'm trying to copy a file from OpenFileDialog to a file path And I'm still a basic so it's hard for me. I google stuffs too but I dont understand that much. Can someone please help me out
private void button2_Click(object sender, EventArgs e)
{
// Show the dialog and get result.
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK) // Test result.
{
string newPath = @"C:\Users\" + un + "\\AppData\\Roaming\\NF";
File.SetAttributes(newPath, FileAttributes.Normal);
File.Copy(openFileDialog1.FileName, newPath, true);
}
}
Upvotes: 0
Views: 2125
Reputation: 216293
The second argument of File.Copy is the name of the file in the new path.
You are passing a directory name.
Add this to your code before copying
string destFile = Path.Combine(newPath, Path.GetFileName(openFileDialog1.FileName));
File.Copy(openFileDialog1.FileName, destFile, true);
Apart from this I recommend to not build your paths using string concatenations. This could be easily a source of errors. Use always the methods available in the class Path
string newPath = Path.Combine("C:\\Users", un, "AppData\\Roaming\\NF");
Upvotes: 4