简体   繁体   中英

Save a file in folder opened by dialog box

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:

  1. If there's a way allowing me to use FolderBowserDialog , that I could get the full path of the location where I save my file?
  2. If there's another method allowing me to copy or download the files from remote, like using SaveFileDialog . The problem is I don't know how to us it to do this.

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;

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);
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM