Reputation: 81
My program has a treeview which lists files from a remote computer. What I need to do is to copy these files from remote into one of my local folders. I wish that when I right click the file in treeview, a dialog box shows up for me to choose a folder, and then I click "OK" in the dialog box, my clicked file could be saved inside that folder.
Since the path of the files in remote is unc path, I'm using
File.Copy(string remote_address, string local_address)
to copy the files. As i said before I need a dialog window to choose folders. So I've tried using a FolderBowserDialog
, however its SelectedPath
property returns me only the path to the folder not including the folder's name! And I haven't found any property to return me the folder's name.
So my questions are:
FolderBowserDialog
, that I could get the full path of the location where I save my file? SaveFileDialog
. The problem is I don't know how to us it to do this.Upvotes: 0
Views: 4456
Reputation: 2296
After you call the FolderBrowserDialog's ShowDialog() method it will return a variable indicating what button the user pressed (ie, Ok or Cancel)
after you make sure the user had used "Ok" to indicate they want to proceed with the operation, you can access the "SelectedPath" field which will give you the full local path they selected.
You can then get the final path by calling
System.IO.Path.Combine(fbd.SelectedPath,remoteFileName);
I'm assuming that fbd is your FolderBrowserDialog instance and remoteFileName should contain just the filename part of the remote file (eg. "MyFile.txt");
If you want to separate the filename from the full remote path, use
var remoteFileName = System.IO.Path.GetFileName(remotePath);
That being said, what a user would typically expect is not a folder browser dialog, but the save file dialog.
you can initialize the save file dialog with a filename, leaving the user to select a folder and possibly change the intended file name as well if they wish.
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = remoteFileName;
sfd.ShowDialog();
sfd.FileName // now contains the full path to the file that the user has selected
don't forget to take the result from the ShowDialog() call to make sure the user didn't cancel out of the save dailog!
Upvotes: 0
Reputation: 23113
The following should work:
var fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == DialogResult.OK)
{
var localPath= Path.Combine(fbd.SelectedPath, Path.GetFilename(remote_address));
File.Copy(remote_address, localPath);
}
Upvotes: 1
Reputation: 5291
I'm not sure which "SavePath" property you are referring to, as FolderBrowserDialog has no such property. The property you are looking for is called SelectedPath
.
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.ShowDialog();
string local_address = dlg.SelectedPath;
Upvotes: 1