none
Why doesn't SaveFileDialog.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" Work?

    Question

  • The code is really straightforward:

    With sfd
          .Title = "Specify Output File."
          .ShowHelp = True
          .OverwritePrompt = True
          .ValidateNames = True
          .Filter = "htm files (*.htm)|*.htm|All files (*.*)|*.*"
          .FilterIndex = 1
          .InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}" 'MyComputer
        End With
    


    And I stepped through it and InitialDirectory starts out as "" and then gets the value which the code specifies.  But when the dialog appears it's not using MyComputer but another (recently used) directory.

    I use this a lot because I prefer for the SaveFileDialog to start in MyComputer and it's always worked in the past.

    Thanks,  Bob
    mercredi 22 juin 2011 20:33

Toutes les réponses

  • Hi,

     

     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
      Me.SaveFileDialog1.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
      If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
       MsgBox(Me.SaveFileDialog1.FileName)
      End If
     End Sub
    


    works as expected, opens MyComputer (Win7 64bit, fw 4.0)

    Edit: I also tested with a real file to be saved and it opens, and reopens after saving at mycomputer.

     

    Regards,

      Thorsten


    mercredi 22 juin 2011 20:46
  • Thanks Thorsten.  If I make a simple test program (fw 3.5) based on your code it runs correctly on my Vista x64 machine.  The app which exhibits the problem I am developing on the same machine and also using fw 3.5.  If I run the same binary on my XP laptop it works as I'd expect it to and as it always has in the past (in other applications).  I am pretty convinced that the problem is not mine.  But still I'd like to be able to get rid of it.

    Bob

    jeudi 23 juin 2011 02:56
  • Also, setting InitialDirectory this way ...

    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

    doesn't help.

    Bob

    jeudi 23 juin 2011 03:29
  • Also, setting InitialDirectory this way ...

    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)

    doesn't help.

    Bob

    Yes, this doesn't do it, because the return-string of Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) is empty.

    Regards,

      Thorsten

    jeudi 23 juin 2011 18:45
  • I could be wrong (since I've never had a problem with this) but it's been reported that this will only work if you enable visual styles (call Application.EnableVisualStyles() from your main method).

    Environment.SpecialFolder.MyComputer doesn't point to an actual physical location, so Environment.GetFolderPath will always return an empty string for it. You have to use the ugly magical string you have above to set it.

    jeudi 23 juin 2011 20:42
  • Some attempted circumventions which HAVE NOT worked ...

    Directory.SetCurrentDirectory("::{20D04FE0-3AEA-1069-A2D8-08002B30309D}")
    Directory.SetCurrentDirectory(Environment.GetFolderPath(Environment.SpecialFolder.MyComputer))

    in both cases Directory.SetCurrentDirectory doesn't like the format of the supplied path.  In the second case it does not like the empty string.  (I tried this before I saw Thorsten's note.)

    HOWEVER, if I set the "InitialDirectory" to a specific, real directory that seems to work, or at least works more often than trying to set it to MyComputer.  I think that I will set both the CurrentDirectory and the dialog.InitialDirectory to the same specific directory for good measure.  But really this area seems to be so messed up that I am not sure that will work consistently.

    Bob

     

     

     

     

    jeudi 23 juin 2011 20:59
  • The OpenFileDialog should work consistently if you choose a directory other than My Computer (keep an eye on the RestoreDirectory option, which may be tripping you up).

    The issue with setting it to My Computer is just that My Computer is not actually a directory. It's somewhat nonsensical to use it as a default path for opening a file, since it isn't a real "path" at all. It should still be possible using that string, but possibly only with visual styles enabled (like here)

    jeudi 23 juin 2011 21:23