예제 #1
0
        //
        // Open the Visual Studio native dialog for selecting a directory
        //
        public static string BrowserFolderDialog(IntPtr owner, string title, string initialDirectory)
        {
            IVsUIShell shell = Package.Instance.UIShell;

            VSBROWSEINFOW[] browseInfo = new VSBROWSEINFOW[1];
            browseInfo[0].lStructSize   = (uint)Marshal.SizeOf(typeof(VSBROWSEINFOW));
            browseInfo[0].pwzInitialDir = initialDirectory;
            browseInfo[0].pwzDlgTitle   = title;
            browseInfo[0].hwndOwner     = owner;
            browseInfo[0].nMaxDirName   = 260;
            IntPtr pDirName = Marshal.AllocCoTaskMem(520);

            browseInfo[0].pwzDirName = pDirName;

            try
            {
                int hr = shell.GetDirectoryViaBrowseDlg(browseInfo);
                if (hr == VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    return(string.Empty);
                }
                ErrorHandler.ThrowOnFailure(hr);
                return(Marshal.PtrToStringAuto(browseInfo[0].pwzDirName));
            }
            finally
            {
                if (pDirName != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pDirName);
                }
            }
        }
예제 #2
0
        public static String BrowseForDirectory(IntPtr owner, string initialDirectory = null, string title = null)
        {
            IVsUIShell uiShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));

            if (null == uiShell)
            {
                using (var ofd = new FolderBrowserDialog()) {
                    ofd.RootFolder          = Environment.SpecialFolder.Desktop;
                    ofd.SelectedPath        = initialDirectory;
                    ofd.ShowNewFolderButton = false;
                    DialogResult result;
                    if (owner == IntPtr.Zero)
                    {
                        result = ofd.ShowDialog();
                    }
                    else
                    {
                        result = ofd.ShowDialog(NativeWindow.FromHandle(owner));
                    }
                    if (result == DialogResult.OK)
                    {
                        return(ofd.SelectedPath);
                    }
                    else
                    {
                        return(null);
                    }
                }
            }
            if (owner == IntPtr.Zero)
            {
                ErrorHandler.ThrowOnFailure(uiShell.GetDialogOwnerHwnd(out owner));
            }
            VSBROWSEINFOW[] browseInfo = new VSBROWSEINFOW[1];
            browseInfo[0].lStructSize   = (uint)Marshal.SizeOf(typeof(VSBROWSEINFOW));
            browseInfo[0].pwzInitialDir = initialDirectory;
            browseInfo[0].pwzDlgTitle   = title;
            browseInfo[0].hwndOwner     = owner;
            browseInfo[0].nMaxDirName   = 260;
            IntPtr pDirName = IntPtr.Zero;

            try {
                browseInfo[0].pwzDirName = pDirName = Marshal.AllocCoTaskMem(520);
                int hr = uiShell.GetDirectoryViaBrowseDlg(browseInfo);
                if (hr == VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    return(null);
                }
                ErrorHandler.ThrowOnFailure(hr);
                return(Marshal.PtrToStringAuto(browseInfo[0].pwzDirName));
            }
            finally {
                if (pDirName != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pDirName);
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Executes Add Search Path menu command.
        /// </summary>
        internal int AddSearchPath()
        {
            // Get a reference to the UIShell.
            IVsUIShell uiShell = GetService(typeof(SVsUIShell)) as IVsUIShell;

            if (null == uiShell)
            {
                return(VSConstants.S_FALSE);
            }
            //Create a fill in a structure that defines Browse for folder dialog
            VSBROWSEINFOW[] browseInfo = new VSBROWSEINFOW[1];
            //Dialog title
            browseInfo[0].pwzDlgTitle = DynamicProjectSR.GetString(DynamicProjectSR.SelectFolderForSearchPath);
            //Initial directory - project directory
            browseInfo[0].pwzInitialDir = _projectDir;
            //Parent window
            uiShell.GetDialogOwnerHwnd(out browseInfo[0].hwndOwner);
            //Max path length
            browseInfo[0].nMaxDirName = NativeMethods.MAX_PATH;
            //This struct size
            browseInfo[0].lStructSize = (uint)Marshal.SizeOf(typeof(VSBROWSEINFOW));
            //Memory to write selected directory to.
            //Note: this one allocates unmanaged memory, which must be freed later
            IntPtr pDirName = Marshal.AllocCoTaskMem(NativeMethods.MAX_PATH);

            browseInfo[0].pwzDirName = pDirName;
            try {
                //Show the dialog
                int hr = uiShell.GetDirectoryViaBrowseDlg(browseInfo);
                if (hr == VSConstants.OLE_E_PROMPTSAVECANCELLED)
                {
                    //User cancelled the dialog
                    return(VSConstants.S_OK);
                }
                //Check for any failures
                ErrorHandler.ThrowOnFailure(hr);
                //Get selected directory
                string dirName = Marshal.PtrToStringAuto(browseInfo[0].pwzDirName);
                AddSearchPathEntry(dirName);
            } finally {
                //Free allocated unmanaged memory
                if (pDirName != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pDirName);
                }
            }
            return(VSConstants.S_OK);
        }