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\";
Related
I'm making a log viewer application in C#.NET/Forms on Win8 desktop.
I have a problem when using the System.Windows.Forms.OpenFileDialog. If I select a file from the %temp% directory and click OK, and then want to select another file, the OpenFileDialog refuse to remember that I last visited the %temp% directory. It always reverts to displaying the last non-%temp% directory I visited, which is very annoying since my app typically opens various log files from %temp%.
Precondition:
- OpenFileDialog created and existing.
- OpenFileDialog has InitialDirectory = "C:\"
Scenario:
- ShowDialog(): Displays C:\ - OK.
- Change directory to C:\logfiles\test.txt and click OK to close.
- ShowDialog(): Displays C:\logfiles\ - OK.
- Change directory to %temp% (which expands to C:\Users\joe\AppData\Local\Temp) and click OK to close.
- ShowDialog(): Displays C:\logfiles\ - FAIL! Here it should show C:\Users\joe\AppData\Local\Temp but it doesn't. It reverts to the last directory I selected that is not in %temp%. Why?
Question: How to prevent this behavior?
You can use InitialDirectory property documented: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory.aspx
Example:
fdlg.InitialDirectory = Path.GetTempPath();
Please check the below link.
OpenFileDialog.RestoreDirectory fails for %temp% location? Bug or Feature
I want my application to allow file attachments, however, I don't want to allow local files to be attached. Obviously, they won't be valid paths on other machines.
Is there anyway I can force the path in an open file dialog to be relative?
I was hoping for a devexpress control that does that, but I can work with the regular Open File Dialog if it's possible.
I can also work with the possibility of only allowing the attachments of URL's.
You could try this:
fileDialog.InitialDirectory = Path.Combine(Application.StartupPath, #"YOUR_SUB_DIRECTORY_NAME");
Okay, so. I'm going to try to explain this as best as I can.
I have been working on a program, obviously. Basically, the user selects one file hits the "Replace" button and it replaces the files in a AppData folder.
Well, I got how to make an AppData folder for my program.
Basically, what I would like to do is first off take the selected file (that's through the open file dialog) and copy to the AppData that I created for one. Then I need to rename the file and copy the file to the other folder.
I've been looking and can't seem to find what I need...which sucks.
EDIT:
My second question. Say the user chooses "myfile.txt" there's a folder in the AppData at ".../Roaming/thefiles/file.txt"
I need to rename and replace that "file.txt", but I can not figure out how to move to that directory since everyone's username is different.
First of all you dont need to create a folder in AppData it will be readily available.
File.Copy(sourcepath,destinationpath); can be used for this purpose
http://msdn.microsoft.com/en-us/library/cc148994.aspx check this out.
use Application.UserAppDataPath or Application.CommonAppDataPath to access your program's app data folder.
you can call File.Copy(sourcepath,Path.Combine(Application.UserAppDataPath,"yourfile.ext"));
Edit
I understand you mean logged in user of the system
using (OpenFileDialog fd = new OpenFileDialog())
{
if (fd.ShowDialog() == DialogResult.OK)
{
string fullFileName = fd.FileName;
string fileNameWithExt = Path.GetFileName(fullFileName);
string destPath = Path.Combine(Application.UserAppDataPath, fileNameWithExt);
File.Copy(fd.FileName, destPath);
}
}
the above code will copy the selected file to AppData path of your Program that belongs to logged in user, ex: if you logged in as user1 to windows this will be copied under user1's AppData
Edit2
If am not mistaken then Application.UserAppDataPath will always gives the path of current logged user of windows, so without worrying of losing other user's data you can safely move the file inside that Directory
In my application I have two places where the user needs to select a file. In both cases, the files are in different directories, but they are generally the same between runs.
The OpenFileDialog appears to be defaulting to the last directory used, but this is useless to me since it is almost always the wrong folder, and I end up alternating between the two folders. What I would like is to somehow have the first dialog remember the path that was used the last time it was opened, and the second to remember its own path as well.
Example: Path A is C:\foo\bar\something\x.dll, Path B is C:\foo\baz\whatever\y.xml
Dialog a opens and I select A, then later dialog b opens(defaulted to A) and I have to navigate back and up to B.
When I open the app again dialog a opens (defaulted to B) and I have to navigate back up to A again.
I would like to avoid all of this extra navigation by remembering the paths separately. Is there a good way to do that?
When you open each dialog, just set the dialog's InitialDirectory property to the folder that was last used for that dialog.
Granted, this will require saving the directory for each dialog, but it will provide the behavior you are looking to achieve.
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.