BROWSE FOLDERS IN THE CLOUD

Giorgio Sfiligoi 166 Reputation points
2021-02-07T08:33:40.427+00:00

My desktop application in C# needs to save certain files in a folder selected by the user.
FolderBrowserDialog can do the job, but only for local folders, it does not 'see' the network and in particular the cloud.
How can I allow the user select a folder e.g. in OneDrive, Dropbox, etc. (provided of course he has an account)?
Ideally, I would like a single browser capable to see both local and network folders.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,922 questions
{count} votes

Accepted answer
  1. Castorix31 85,136 Reputation points
    2021-02-07T11:03:57.553+00:00

    I did another test with SHBrowseForFolder, but not sure it will work for you,
    as I also get OneDrive with FolderBrowserDialog on my configuration (Windows 10, .NET 4.7.2) =>
    (selected folder in sSelectedPath, add a button for the click...)

    public partial class Form1 : Form  
    {  
        public enum HRESULT : int  
        {  
            S_OK = 0,  
            S_FALSE = 1,  
            E_NOINTERFACE = unchecked((int)0x80004002),  
            E_NOTIMPL = unchecked((int)0x80004001),  
            E_FAIL = unchecked((int)0x80004005),  
        }  
      
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]  
        public class BROWSEINFO  
        {  
            public IntPtr hwndOwner;  
            public IntPtr pidlRoot;  
            public IntPtr pszDisplayName;  
            public string lpszTitle;  
            public int ulFlags;  
            public BrowseCallbackProc lpfn;  
            public IntPtr lParam;  
            public int iImage;  
        }  
      
        public delegate int BrowseCallbackProc(IntPtr hwnd, int msg, IntPtr lParam, IntPtr lpData);  
      
        [DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);  
      
        // Browsing for directory.  
        public const int BIF_RETURNONLYFSDIRS = 0x00000001;  // For finding a folder to start document searching  
        public const int BIF_DONTGOBELOWDOMAIN = 0x00000002;  // For starting the Find Computer  
        public const int BIF_STATUSTEXT = 0x00000004;   // Top of the dialog has 2 lines of text for BROWSEINFO.lpszTitle and one line if  
                                                        // this flag is set.  Passing the message BFFM_SETSTATUSTEXTA to the hwnd can set the  
                                                        // rest of the text.  This is not used with BIF_USENEWUI and BROWSEINFO.lpszTitle gets  
                                                        // all three lines of text.  
        public const int BIF_RETURNFSANCESTORS = 0x00000008;  
        public const int BIF_EDITBOX = 0x00000010;   // Add an editbox to the dialog  
        public const int BIF_VALIDATE = 0x00000020;   // insist on valid result (or CANCEL)  
      
        public const int BIF_NEWDIALOGSTYLE = 0x00000040;   // Use the new dialog layout with the ability to resize  
                                                            // Caller needs to call OleInitialize() before using this API  
      
        public const int BIF_USENEWUI = (BIF_NEWDIALOGSTYLE | BIF_EDITBOX);  
      
        public const int BIF_BROWSEINCLUDEURLS = 0x00000080;   // Allow URLs to be displayed or entered. (Requires BIF_USENEWUI)  
        public const int BIF_UAHINT = 0x00000100;   // Add a UA hint to the dialog, in place of the edit box. May not be combined with BIF_EDITBOX  
        public const int BIF_NONEWFOLDERBUTTON = 0x00000200;   // Do not add the "New Folder" button to the dialog.  Only applicable with BIF_NEWDIALOGSTYLE.  
        public const int BIF_NOTRANSLATETARGETS = 0x00000400;   // don't traverse target as shortcut  
      
        public const int BIF_BROWSEFORCOMPUTER = 0x00001000;  // Browsing for Computers.  
        public const int BIF_BROWSEFORPRINTER = 0x00002000;  // Browsing for Printers  
        public const int BIF_BROWSEINCLUDEFILES = 0x00004000;  // Browsing for Everything  
        public const int BIF_SHAREABLE = 0x00008000;  // sharable resources displayed (remote shares, requires BIF_USENEWUI)  
        public const int BIF_BROWSEFILEJUNCTIONS = 0x00010000;  // allow folder junctions like zip files and libraries to be browsed  
      
        [DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath);  
      
        [DllImport("Shell32.dll", SetLastError = true,CharSet = CharSet.Unicode)]  
        public static extern HRESULT SHILCreateFromPath([MarshalAs(UnmanagedType.LPWStr)] string pszPath, ref IntPtr ppIdl, ref uint rgflnOut);  
      
        [DllImport("Shell32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern HRESULT SHGetKnownFolderIDList(ref Guid rfid, uint dwFlags, IntPtr hToken, ref IntPtr ppidl);  
      
        public enum KNOWN_FOLDER_FLAG  
        {  
            KF_FLAG_DEFAULT,  
            KF_FLAG_FORCE_APP_DATA_REDIRECTION,  
            KF_FLAG_RETURN_FILTER_REDIRECTION_TARGET,  
            KF_FLAG_FORCE_PACKAGE_REDIRECTION,  
            KF_FLAG_NO_PACKAGE_REDIRECTION,  
            KF_FLAG_FORCE_APPCONTAINER_REDIRECTION,  
            KF_FLAG_NO_APPCONTAINER_REDIRECTION,  
            KF_FLAG_CREATE,  
            KF_FLAG_DONT_VERIFY,  
            KF_FLAG_DONT_UNEXPAND,  
            KF_FLAG_NO_ALIAS,  
            KF_FLAG_INIT,  
            KF_FLAG_DEFAULT_PATH,  
            KF_FLAG_NOT_PARENT_RELATIVE,  
            KF_FLAG_SIMPLE_IDLIST,  
            KF_FLAG_ALIAS_ONLY  
        }          
      
      
        public Form1()  
        {  
            InitializeComponent();  
        }  
      
        private void Form1_Load(object sender, EventArgs e)  
        {  
      
        }  
      
        private void button1_Click(object sender, EventArgs e)  
        {  
            //FolderBrowserDialog fbd = new FolderBrowserDialog();  
            //if (fbd.ShowDialog() == DialogResult.OK)  
            //{  
            //    string sSelectedPath = fbd.SelectedPath;  
            //}  
            //return;  
      
            HRESULT hr = HRESULT.E_FAIL;  
            IntPtr pidlRet = IntPtr.Zero;  
            BROWSEINFO bi = new BROWSEINFO();  
            bi.hwndOwner = this.Handle;  
            bi.lpszTitle = "Title";  
            bi.ulFlags = BIF_SHAREABLE | BIF_USENEWUI;  
            IntPtr pidlRoot = IntPtr.Zero;  
            //uint nrgflnOut = 0;  
            //hr = SHILCreateFromPath("shell:::{35786D3C-B075-49b9-88DD-029876E11C01}", ref pidlRoot, ref nrgflnOut);  
            //if (hr == HRESULT.S_OK)  
            //    bi.pidlRoot = pidlRoot;  
      
            // Test OneDrive as root  
            //Guid FOLDERID_SkyDrive = new Guid("A52BBA46-E9E1-435f-B3D9-28DAA648C0F6");  
            //hr = SHGetKnownFolderIDList(ref FOLDERID_SkyDrive, (uint)KNOWN_FOLDER_FLAG.KF_FLAG_DEFAULT, IntPtr.Zero, ref pidlRoot);  
            //if (hr == HRESULT.S_OK)  
            //    bi.pidlRoot = pidlRoot;  
              
            pidlRet = SHBrowseForFolder(bi);  
            if (pidlRet != IntPtr.Zero)  
            {  
                IntPtr pszSelectedPath = Marshal.AllocHGlobal(((260 + 1) * Marshal.SystemDefaultCharSize));  
                bool bRetPath = SHGetPathFromIDList(pidlRet, pszSelectedPath);  
                if (bRetPath)  
                {  
                    string sSelectedPath = Marshal.PtrToStringUni(pszSelectedPath);  
                }  
                Marshal.FreeHGlobal(pszSelectedPath);  
            }  
        }  
    }  
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.