How do I open a FolderBrowserDialog at the selected folder?

后端 未结 9 1909
深忆病人
深忆病人 2020-12-15 18:00

I have a FolderBrowserDialog, displayed with code shown below. However, it keeps opening with \'Computer\', i.e. the root of the folder tree, selected. How do I get it to

9条回答
  •  清酒与你
    2020-12-15 18:11

    I was seeing this problem using:

    RootFolder = Environment.SpecialFolder.MyComputer
    SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    

    By playing around with hard-wired versions of that path's subpaths and sibling branches, I decided this really is a permissions thing. It doesn't work with C:\Users[UserID]\Documents, but it works fine with C:\Users, or C:\Users\Public\Documents (even though that doesn't exist or, at least it's called Public Documents instead of Documents), or C:\Users\Default\Documents (even though that doesn't exist or, at least it's called My Documents instead of Documents).

    What I ended up doing was checking whether the SelectedPath contains "\users[UserID]", then using Environment.SpecialFolder.Desktop if it did and Environment.SpecialFolder.MyComputer if it didn't.

    Probably, checking the SelectedPath folder permissions would be a better test, but this will do for the expected use cases.

提交回复
热议问题