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:
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?
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.
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);
}
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;
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!
Related
I have some data and I would like to export it to excel. I did all the code, and everything is working fine, Now I want to save that excel file to the hard drive. I could do that too. but my problem is that I couldn't know how to allow the customer to set his/her own file name.
What I have tried:
FolderBrowserDialog brwsr = new FolderBrowserDialog();
//Check to see if the user clicked the cancel button
if (brwsr.ShowDialog() == DialogResult.Cancel)
return;
else
{
string newDirectoryPath = brwsr.SelectedPath;
//Do whatever with the new path
}
the problem of that method is it is just allows the users to select the folder that the want the file to be saved to. I want to all the user to specifiy the path and the file name.
Any idea to help pleaes?
many thanks
You need to use a SaveFileDialog instead. This let's the user specify a path + filename. Check this out for more info: SaveFileDialog on MSDN
SaveFileDialog is pretty similar to FolderBrowserDialog, so you can almost replace the existing code you already have ;)
Specify File Type (EDIT)
Have a look a the Filter property.
Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*
I am using on WPF Win32 file browser dialog to select a file in the filesystem. Now my problem is, when I select an shortcut file ending with .ink, it shows the application path not the shortcut(.ink) path.
For example I want to select snoop shortcut
as filepath, I've got.
How can I get the path from shortcut not from application?
Update
I am trying restrict with
Win32.OpenFileDialog ofd = new Win32.OpenFileDialog();
ofd.Filter = "Link (*.lnk)|*.lnk";
but only .lnk it is possible to select. It should be possible to select other files too, not only .lnk file.
You must use OpenFileDialog.DereferenceLinks property.
Gets or sets a value indicating whether a file dialog returns either the location of the file referenced by a shortcut or the location of the shortcut file (.lnk).
odf.DereferenceLinks = false;
For the update question, just add All Files|*.*| :
ofd.Filter = "All Files|*.*|Link (*.lnk)|*.lnk";
I am trying to replicate the standard save file process where, if there already exists a file with the necessary file extension, it is pre-selected in the SaveFileDialog. In my program I am using SaveFileDialog to allow the user to select the path for the file on the system. Upon save, it will automatically open to the path that the last saved file was, but the user has to re-select it.
Here is where I think something like this would come into play in my code:
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = ".cct"; //The file extension
Nullable<bool> result = dlg.ShowDialog(); //Lets user select path
//**I'm guessing that the "pre-select" operation I am talking about
//would go here
directory = Path.GetDirectoryName(dlg.FileName); //Directory = File path on system
I have looked at the CheckFileExists property and it says that it "Gets or sets a value indicating whether a file dialog displays a warning if the user specifies a file name that does not exist." So it looks like it's more focused around checking whether or not the user enters a new file name.
How do I make it so that the previously saved file with the same extension is highlighted or selected in the SaveFileDialog if one is found upon save?
You can do a couple of things.
You could restore the path the user last saved with by setting dlg.RestoreDirectory = true.
Set the FileName before you show the dialog and it will automatically show that folder and insert the file name using dlg.FileName
http://msdn.microsoft.com/en-us/library/microsoft.win32.savefiledialog(v=vs.110).aspx
I think that all i need is in the question. I placed my method in my Form.Load I can either create a folder or open a SaveFileDialog but not both at once.
If someone could help me please.
Thanks.
SaveFileDialog lets the user choose a file location that already exists. If not, they can create a folder within the dialog as #Bali suggested.
If you want the user to be able to create a new folder without using the dialog then you'll need to let the user type the path (e.g. in a textbox). Then you can check to see if the directory exist using Directory.Exist, and if not, create it using Directory.Create.
void CheckPath(string path)
{
var dir = Path.GetDirectoryName(path);
if( !String.IsNullOrEmpty(dir) && !Directory.Exists(dir))
Directory.Create(dir);
}
Open a FolderBrowserDialog for the user with the title (Description property) set to something like "Choose an existing folder or create a new one". Do not forget to set its ShowNewFolderButton property to true.
You can also use the FolderBrowserDialog to ask the user only to select the containing ("parent") folder, and create the new folder yourself by calling Directory.CreateDirectory. In this case, ShowNewFolderButton should be false.
This would be to create a new directory
Directory.CreateDirectory(#"C:\Your File Path Here");
This would be to open a file. You can select where it opens the initial directory of the file by changing the path.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = (#"C:\Your starting File Path");
openFileDialog1.Filter = "All Files (*.*)|*.*";
openFileDialog1.Title = "Select a File";
In a WPF app I need to make it possible for a user to pick a file by the standard Open File Dialog and save it to the predefined folder (user doesn't know where it is) right after the user click OK button on Open File Dialog. Something like of importing a file to the application. I do it in the following way:
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Text documents (.txt)|*.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.SafeFileName;
System.IO.File.Copy(filename, #"E:\TestFolder\" + filename);
MessageBox.Show("File " + filename + " saved");
}
Is there a standard way to check if the file already exists before trying to save it and if it is really saved after saving it?
Look at System.File.Exists that should be able to tell you what you need to know.
The System.IO.File.Exists method returns true if a file at the given path exists, so you could use it to check both before and after your copy operation.
Use the SaveFileDialog (Microsoft.Win32). If you try to save over a file that already exists it will prompt you to make sure you want to save over that file. This doesn'y actually save it though, all it will do is provide the name and location of the file you want to create/save over. After you use the SaveFileDialog to select the file you then need to do the work of saving the file.
This post may be helpful