Copy File via Save Dialog Box
The program that i wanted to do is that the application uses a Save Dialog box then chooses the directory where the user wants to copy a file... after that, the file is copied to the selected directory. Although i'm having an error "the file does not exist" when i use the Save Dialog box. But when i try not to use a dialog box and use a specific directory where I want to save a copied file, it works fine... Can anybody provide me a working code for it?
Silvyster AbingPosted Feb 25, 2008, 6:07 PM
AlanPosted Feb 25, 2008, 10:19 AM
To start from the path in which the .exe resides, you could use this line:
string fileToCopy = Application.StartupPath + "\\" + "myfiletocopy.txt";Silvyster AbingPosted Feb 25, 2008, 7:30 AM
AlanPosted Feb 25, 2008, 5:47 AM
Try something like this:
string
fileToCopy = @"c:\documents\myfiletocopy.txt"; // or whatever SaveFileDialog sfd = new SaveFileDialog();sfd.FileName = fileToCopy;
sfd.Filter =
"Text files (*.txt)|*.txt|All files (*.*)|*.*";sfd.FilterIndex = 1;
sfd.RestoreDirectory =
true; if (sfd.ShowDialog() == DialogResult.OK){
System.IO.
File.Copy(fileToCopy, sfd.FileName, true);}