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.
Related
I want to avoid opening the Save As dialog in one specific folder. If I open a file from that folder then do a Save As…, it starts in that same folder (as expected). I thought I could just examine InitialDirectory after calling new SaveFileDialog() and change it if necessary, but it is an empty string.
Directory.GetCurrentDirectory() returns the folder containing the executable.
var dialog = new SaveFileDialog();
Console.WriteLine(Directory.GetCurrentDirectory()); // Prints "Z:\Documents\Projects\ProjectName\bin\x64\Debug"
Console.WriteLine(dialog.InitialDirectory); // Prints empty string
How do I ask Windows (7 or 10) which folder the Save As… dialog will start in?
Edit
This is a completely different question than "How do I set the initial directory?". I want to know what the initial directory is going to be before the dialog opens so that I can change it only in the case where it is going to be one specific directory.
I assume that you want the savefiledialog to start from the folder you want
Try this to change the initial directory to whatever you want
dialog.InitialDirectory="C:\Users\Your_Name\Desktop\";
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 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.
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.
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.