Click here to Skip to main content
15,889,892 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When setting the SaveFileDialog's InitialDirectory is there a way to prevent user from navigating to another directory?
Posted

The FileDialog classes are not designed this way.

The only way I can see is writing your own specialized control which does not allow to move out of initial property in principle. Doing so is easy enough. It could be based in System.Windows.Forms.ListView.

The files should be listed using System.IO.Directory.GetFiles. You should not look for directory, but you can use file mask specific to your application.

See this discussion to avoid a common pitfall discussed there: Directory.Get.Files search pattern problem[^].

There is one more important but advanced feature? What if is another process modifies the directory you're working with? You need some event to track is. For this purpose, use System.IO.FileSystemWatcher.

—SA
 
Share this answer
 
Comments
Nish Nishant 14-Feb-11 16:34pm    
Voted 5, although I would personally just prompt for filename.
Sergey Alexandrovich Kryukov 14-Feb-11 16:37pm    
Thank you.
Maybe you're right, for your own use, but I understand why OP wants the UI as well. So, I tried to solve that exact problem: it does not look unreasonable or not feasible (not even hard to implement).
--SA
Espen Harlinn 14-Feb-11 17:05pm    
Good reply, my 5
Sergey Alexandrovich Kryukov 14-Feb-11 17:14pm    
Thank you, I think this is the most realistic way, if OP really want a solution.
--SA
MCY 14-Feb-11 18:08pm    
true, seems the easiest (and classiest) way. my 5
Take a look at FileDialog.HookProc[^]

A bit more in the way of documentation here:
http://msdn.microsoft.com/en-us/library/ms646931(VS.85).aspx[^]

This should allow you to detect when the user changes directory, so that you can change it back to your preffered directory.

I suspect that you will find it easier to implment your own form as SAKryukov suggested.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Nish Nishant 14-Feb-11 17:34pm    
Good answer, voted 5. I completely forgot that you can hook the dialog proc in .NET too. I am going to delete my answer now, or at least edit out the erroneous hook info. Thanks Espen.
Espen Harlinn 14-Feb-11 17:43pm    
Thank you Nishant!
Sandeep Mewara 14-Feb-11 23:59pm    
5! new to me and good one. :)
Espen Harlinn 16-Feb-11 11:21am    
Thank you Sandeep!
JF2015 16-Feb-11 0:56am    
Didn't know this one - I like it.
Hi,

One possibility is to use RestoreDirectory property, but if the path cannot be changed by the user, would it be simpler just to ask for the name of the file instead of using SaveFileDialog?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Feb-11 16:33pm    
I don't think this would help. I think the easiest and more realistic way it to write one's own component (greatly simplified compared to normal file dialog because only one directory is required); please see my answer.
--SA
Wendelius 14-Feb-11 16:46pm    
I guess it depends on the requirements. UI is required anyway so basically the next thing would be to decide what controls to include to the UI. In the simplest form just a text box for the file name is adequate but if the user is allowed to pick an existing file, adding a ListView as you suggested is a good idea.

mika
You can't really do that from the .NET classes. Had this been native code, you could have hooked the dialog proc and that would have given you some options. [Okay, that was wrong. .NET lets you hook the dialog proc too, see Espen's answer.]

Since you are already decided on the folder, why even show this dialog? Just prompt for filename and then save it to the pre-decided directory.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 14-Feb-11 16:32pm    
Nishant, writing one's own control for this kind of navigation is quite feasible; please see my answer.
--SA
Kind of a kludge, but you could always replace the selected directory with the mandatory one after they exit the dialog to pick the filename by rebuilding the path+file using a string with your mandatory path concatenated to the Path.GetFileName(<user's choice>).

If you're going to do that, you're reducing the fileSaveDialog control to a fancy text-input box. Why not just ask for the filename in a text box, then validate as a usable file name using RegEx?
 
Share this answer
 
v2
Comments
Richard Deeming 14-Apr-15 12:15pm    
This question was solved in 2011!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900