How to retrieve the openfiledialog opening path? C# - c#

I have a openfiledialog in my application. I do not set the initial directory and user can navigate to open file and import it to the application (for example imports excel file from C:\ Test location). Next time, when user wants to open another file, system (by default, since initial directory is not set) remember the previous location and openfiledialog opens in that directory (C:\ Test). Is there anyway to retrieve the path that openfiledialog opens? I am sorry if I couldn't explain my problem well and thanks for the help.

Registry is hard to handle, I would suggest that you use Application Properties instead.

Related

Get last used directory

when the SaveFileDialog opens for the first time it has an initial directory. I would like to get that path.
And also if I go into C:\Temp and save a file. Next time I open the SaveFileDialog the program will remember this. I would like to have this path also. Or do I have to save this manual in my program?
Best regards
R
You have to set FileDialog.RestoreDirectory to true.

Folder Picker Dialog

I am currently using FolderBrowserDialog to select a folder in my .NET solution. However, this dialog does not allow me to select SharePoint folders, just local or network folders.
I would like to use something similar to SaveFileDialog or OpenFileDialog, because they allow browsing SharePoint folders. However, these options seem to require that you specify or select a specific file, rather than a folder.
Is there a way to use create a folder picker similar to SaveFileDialog or OpenFileDialog? Ideas in VB.NET or C# are welcome.
UPDATE:
As a workaround, I am now using the folder picker from an Office application, like this:
app.FileDialog(Office.MsoFileDialogType.msoFileDialogFolderPicker)
where app can be the Excel, PowerPoint, Word, etc. application object. I would still prefer not to have to reference an Office app to pull this off, but it's the only option I have found so far. I did not submit this as the answer because I am hoping for a real solution, rather than a workaround.
If OpenFileDialog and SaveFileDialog works for you you can select a file and then use the command
C#
Path.GetDirectoryName(path_to_file);
or in VB.net
Path.GetDirectoryName(path_to_file)
I don't know if it fill your needs... It depends where you need this code and if the folder starts empty or not, as you know if the folder starts empty this method will not work
This if you decide to stop using
Office.MsoFileDialogType

SaveFileDialog keeping folder in memory

So I'm using a SaveFileDialog to save a file to a location the user selects. After the saving has occurred, the user is able to do what they want with the file and folder they saved to.
However, the user cannot delete or modify the folder...it's being held in memory by the application so when another SaveFileDialog is opened, it can point to that same directory.
Our application is fairly complex and I don't want this sort of feature enabled. Is there a way to turn it off?
Thank you in advance.
Short from mis-behaving shell extensions, this is normal. SFD will change the current working directory of your program to the directory that contains the file selected by the user. And that puts a lock on the directory.
You avoid this by setting the RestoreDirectory property to True.
Make sure you're disposing the stream or whatever resource you're using to actually write the file. This resource is most likely staying open locking the folder and file. Read about the using() syntax here http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx.

Quick Question: How can I make sure the current directory is not changed because of OpenFileDialog?

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.

StreamReader looking for file in the wrong directory in C#

I have a program where I am using windows form, in that form I use openFileDialog where I open a file in some directory. Then I use in a different function a StreamReader and I have a 2nd file in my big/debug directory which I want the streamReader to open. But for some reason after I open the 1st file with the openFileDialog the StreamReader looks for the 2nd file in that directory instead in bin/debug as usual.
Does anyone know why he does that and how can I solve it?
Thanks in advance,
Greg
The OpenFileDialog has that behavior; it alters the current directory for the application. To prevent this from happening, you can use the RestoreDirectory property of the OpenFileDialog.
When you change directory in an open file dialog, this also causes your application's working directory to change. So if you are trying to use relative paths, it will look in the wrong place.
The solution is RestoreDirectory.
If you don't specify a complete file path but only a file name, that means that the file is in the current directory. When you use the OpenFileDialog, it changes the current directory.
If you want to access a file somewhere regardless of what the current directory is set to, you have to specify a complete path for it. You can use Application.StartupPath to get the path to the folder where your program is.

Categories