SaveFileDialog doesnt show up in Vista

Recently I was writing an Add-In for Visual Studio 2008 and I came across requirement for saving a file as an Excel file. No big deal as it looked to me!!

I happily wrote the helper function, but to surprise, it didn’t work on Vista SP1 while it was working on Windows Server 2003. The dialog box never shows up and surprisingly, not even an exception was thrown! (Maybe it would be throwing unmanaged exception, I didn’t catch that)

I thought it’s something to do with LUA (Least User Access, Vista Security you know!), vista doesn’t directly allow write access to windows drive (i.e. C :\), so I changed the initial directory. But still it’s the same.

Then I realized that SaveFileDialog (all FileDialog Childs for that matter) requires higher trust level (for full write access on the drive) otherwise it’s a security issue, you start with some directory as initial directory and navigate to some other directory fool user to click on some malicious program. That makes sense to me, I thought maybe as visual studio add-in my application was running with lower trust level so dialog box doesn’t open up. I opened caspol and again assigned full trust to my signed assembly (though it was already running with full trust), hoping it would help, I tried again and but hit the roadblock and the same problem!!

I search, couldn’t get much, so after lot of hit and trail found that these three properties did the trick.

            DialogSave.ShowHelp = true;

            DialogSave.CreatePrompt = true;

            DialogSave.OverwritePrompt = true;

 

Couldn’t find much on internet regarding this, so posting now…

Here is the code that worked for me J

        private static string CreateNewFile()

        {

            string filepath = "";

            SaveFileDialog DialogSave = new SaveFileDialog();

            DialogSave.DefaultExt = "xls";

            DialogSave.Filter = "Excel file (*.xls)|*.xls|All files (*.*)|*.*";

        DialogSave.AddExtension = true;

            DialogSave.RestoreDirectory = true;

            DialogSave.Title = "Where do you want to save the RTM report?";

            DialogSave.InitialDirectory = @"C:/";

            DialogSave.ShowHelp = true;

            DialogSave.CreatePrompt = true;

            DialogSave.OverwritePrompt = true;

            if (DialogSave.ShowDialog() == DialogResult.OK)

            {

                filepath = DialogSave.FileName;

            }

            DialogSave.Dispose();

            DialogSave = null;

            return filepath;

      }

Comments

  • Anonymous
    August 28, 2008
    PingBack from http://informationsfunnywallpaper.cn/?p=2719

  • Anonymous
    December 21, 2008
    Thanks a lot!.....I had the same problem!!!

  • Anonymous
    June 01, 2009
    It worked for me too. Thanks a lot!

  • Anonymous
    October 13, 2010
    The comment has been removed