I am currently working on a C# WPF project. I have a FolderBrowserDialog in the System.Windows.Forms namespace. I am creating an instance of the dialog with a variable named dlg and assigning the selected path to My Documents by using the following line of code:
dlg.SelectedPath = Environment.SpecialFolder.MyDocuments.ToString();
However, this doesn't seem to make much difference. I then tried doing the same thing but with the root path but this just seems to make it that it will set the root as My Documents and you can't get out of My Documents, i.e. to C:\ or Desktop.
How can I set a default path but still allowing access to all available areas of the drive, e.g. default path to be My Documents but allow the user to go outside of My Documents to C:\ or desktop.
Thanks for any help you can provide.
You are assigning the wrong value to SelectedPath. By setting Environment.SpecialFolder.MyDocuments.ToString(), you are setting the string "MyDocuments" (or "Personal" as it has the same value in the Environment.SpecialFolder enum) to the SelectedPath. This cannot be found as it is not a valid path, so nothing gets selected.
You need to lookup the path of the special folder using Environment.GetFolderPath():
dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
This will set the path of the special folder, which the folder browse dialog will select when it opens.
Related
I'm using System.Net.FtpClient to work with FTP server in my web application.
After creating a directory and set it as working directory and uploading files in it, I want to go back one step (the parent directory) to do the same for other folders and files, but I don't know how to get the parent and set it as working directory.
I assume you use a relative path to enter the subdirectory like below.
ftpClient.SetWorkingDirectory("subdir");
Were you using an absolute path, I assume you would know how to open the parent directory.
To enter a parent directory, use "..":
ftpClient.SetWorkingDirectory("..");
See Representations of paths by operating system and shell on Wikipedia.
You can always use the absolute path:
ftpClient.SetWorkingDirectory("/path/to/parent/directory");
I'm creating a program that controls if words in a textbox are present in a text file.
The question is: Is there a way to find the path of that file without depending on the computer you're on?
If you don't use an absolute value for the path - it'll always try to resolve it relative to where your application is.
So if you put the file in the same folder as your application, it'll find it.
Otherwise, if you want your user to locate the file for you, you could use an OpenFileDialogExample here
Another option is to use one of the 'known' paths (such as My Documents). You can do this using Environment.GetFolder
But all of these depend on what you're trying to do exactly.
You can use relative paths. For example, if your path is "file.txt" and save the file, it will be saved next to your exe. Same thing for opening.
I'm supporting an old version of a C# application, running on .NET 3.5. We've found an issue with the FolderBrowserDialog on Windows Vista (either 32 or 64-bit).
Basically what happened is that the dialog would appear, but only the root Desktop node would be shown, not even able to expand it to show anything else. Obviously, that's impossible to use.
After a huge amount of trial and error I eventually managed to get something useable, by setting the RootFolder property before the rest of the setup:
FolderBrowserDialog browsePath = new FolderBrowserDialog();
browsePath.RootFolder = Environment.SpecialFolder.MyComputer;
browsePath.SelectedPath = this.textBoxTo.Text;
browsePath.Description = TextResources.OutputTargetCaption;
browsePath.ShowNewFolderButton = true;
if(browsePath.ShowDialog(this) == DialogResult.OK)
{
this.textBoxTo.Text = UpdateLocation(browsePath.SelectedPath);
}
This almost works; however, I've got the bizarre issue that then the SelectedPath (by definition the contents of textBoxTo) is a path to within the current user's home directory, it won't automatically browse to that path, instead just showing the My Computer node expanded to one level. It's perfectly fine for any other path.
I'm sure your first guess would be a permissions issue, as was my intuition. It doesn't appear to be, this issue occurs running normally and as an Administrator, for both standard and Administrator accounts. It's a clean install, of course, no weird permissions or anything.
This is pretty annoying when all of our defaults are within the current user's directory!
Note: This only happens within the application; it's not reproducible with a small test application, as far as I've seen.
Any ideas on what could be causing this?
Update: Screenies:
This is the behaviour I want (obtainted from a little test app)
This is what I get with the default property
This is what I get by setting the root to My Computer
Note: The last image had the same SelectedPath set as the expected image...
I had a similar problem. In Windows Vista and Windows 7 the following code:
browsePath.RootFolder = Environment.SpecialFolder.MyComputer;
returns the Desktop. If you look in Windows Explorer, the root of the tree is Desktop, not My Computer like it was in Windows XP.
To solve this problem use this instead:
browsePath.RootFolder = #"C:\";
Every Windows computer has a C:\ drive, so this will solve your problem.
I hope this helps you.
If you're only accessing the users private folders, use
browsePath.RootFolder = Environment.SpecialFolder.Personal
Only the specified folder and any subfolders that are beneath it will
appear in the dialog box and be selectable. The SelectedPath property,
along with RootFolder, determines what the selected folder will be
when the dialog box is displayed, as long as SelectedPath is an
absolute path that is a subfolder of RootFolder (or more accurately,
points to a subfolder of the shell namespace represented by
RootFolder).
In short, you can not input someones private folder as a startup selectedPath unless RootFolder is inside the current users private folder already.
For details, see: http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.rootfolder.aspx
VB.NET code
Dim fdb As New FolderBrowserDialog
With fdb
'.RootFolder = Environment.SpecialFolder.MyComputer
'this folder don't exists in vista, the my computer folder was renamed to computer (in spanish "mi pc" to "equipo")
'try with another initial folder
.RootFolder = Environment.SpecialFolder.Desktop
'You can set the desktop as home directory because users typically already have shortcuts or the left side menu to navigate
Dim dr As DialogResult = .ShowDialog
If _
dr = DialogResult.OK Or _
dr = DialogResult.Yes Then _
If IO.Directory.Exists(.SelectedPath) = True Then _
Me.textBoxTo.Text = UpdateLocation(.SelectedPath)
End With
basically, try another directory and make sure the chosen directory exists.
if you're still having problems, is probably due to some fault in the system.
Is there anyway to set the folderBrowserDialog to start in a set folder on a network drive? I know it takes Environment.SpecialFolders but I'm not seeing what I need in there...
Set SelectedPath to a path on the network.
I don't think it's possible to a folder picker rooted to a network path.
If you mean, the FolderBrowserDialog should start with a specific folder already selected, then set the SelectedPath property before calling ShowDialog.
If you mean rooted to a specific folder, the FolderBrowserDialog can only be rooted to one of the SpecialFolders values.
Alternately, use a control like FolderView which allows rooting to a random folder. Further, its a control, so you can place it in your own form.
DISCLAIMER: I work for LogicNP Software, the developers of FolderView.
say my application is installed in C:\programfiles\xyz\ I've some setting & other data files (*.dat files) in this directory too.
My application uses OpenFileDialog to open an image. Problem is that when ever user browses to some directory (say MyPictures) for an image. The current working directory of the application becomes that directory (MyPictures in this case).
After user inputs the image. I do some processing over it and save some values to imagedata.dat which will be located in the path where original application is installed.(C:\programfiles\xyz here )
In my code I'm just using FileStream("imagedata.dat",...,...) without any path prefix before the filename. Problem here is that application is crashing because it is searching for imagedata.dat in 'MyPictures\imagedata.dat'.
How can I avoid this?
You should be using absolute path names when saving data to files. The current working directory is controlled by the user, not by you (for example, if they launch your process from a shortcut then the working directory could've been changed before your process even starts up).
Also, you should never save anything under C:\Program Files during normal use. Doing this means your program needs to be running as an administrator, and unless you're doing administrator-y things then you should be able to run it as a regular user.
The correct thing to do in your case is to use the Environment.GetFolderPath() function to get the location of the ApplicationData folder and save your data under there. Just choose a sub-directory based on your application's name.
You could save it to GetCurrentDirectory then restore with SetCurrentDirectory. However, I agree wih codeka that using the appropriate GetFolderPath (probably ApplicationData, CommonApplicationData or LocalApplicationData) is a better solution.