/// <summary> /// Brings up the browse folder dialog. /// </summary> /// <param name="sender">The browse button.</param> /// <param name="e">The <see cref="EventArgs"/> object that contains the event data.</param> private void OnBrowseButtonClick(object sender, EventArgs e) { // initialize the dialog to the current file (if it exists) string fullPath = this.FullPath; if (File.Exists(fullPath)) { this.fileBrowserDialog.InitialDirectory = Path.GetDirectoryName(fullPath); this.fileBrowserDialog.FileName = Path.GetFileName(fullPath); } // show the dialog if (this.fileBrowserDialog.ShowDialog(this) == DialogResult.OK) { fullPath = this.fileBrowserDialog.FileName; string path = fullPath; if (MakeRelative && !string.IsNullOrEmpty(RootFolder)) { string rootFolder = Path.GetFullPath(RootFolder); if (Directory.Exists(rootFolder)) { if (!rootFolder.EndsWith(Path.DirectorySeparatorChar.ToString()) && !rootFolder.EndsWith(Path.AltDirectorySeparatorChar.ToString())) { rootFolder = rootFolder + Path.DirectorySeparatorChar; } path = new Url(rootFolder).MakeRelative(new Url(fullPath)); } } this.fileTextBox.Text = path; } }
/// <summary> /// Brings up the browse folder dialog. /// </summary> /// <param name="sender">The browse button.</param> /// <param name="e">The <see cref="EventArgs"/> object that contains the event data.</param> private void OnBrowseButtonClick(object sender, EventArgs e) { // initialize the dialog to the current directory (if it exists) bool overridePersistedInitialDirectory = false; string initialDirectory = null; if (Directory.Exists(FullPath)) { initialDirectory = FullPath; overridePersistedInitialDirectory = true; } IntPtr parentWindow = Handle; Guid persistenceSlot = typeof(FileBrowserTextBox).GUID; IVsUIShell2 shell = (IVsUIShell2)Package.GetGlobalService(typeof(SVsUIShell)); // show the dialog string path = shell.GetDirectoryViaBrowseDialog(parentWindow, persistenceSlot, "Select folder", initialDirectory, overridePersistedInitialDirectory); if (path != null) { if (MakeRelative && !string.IsNullOrEmpty(RootFolder)) { string rootFolder = Path.GetFullPath(RootFolder); if (Directory.Exists(rootFolder)) { if (!rootFolder.EndsWith(Path.DirectorySeparatorChar.ToString()) && !rootFolder.EndsWith(Path.AltDirectorySeparatorChar.ToString())) { rootFolder = rootFolder + Path.DirectorySeparatorChar; } path = new Url(rootFolder).MakeRelative(new Url(path)); } } this.folderTextBox.Text = path; } }
public static bool ContainsInvalidFileNameChars(string name) { if (String.IsNullOrEmpty(name)) { return(true); } try { if (Path.IsPathRooted(name) && !name.StartsWith(@"\\", StringComparison.Ordinal)) { string root = Path.GetPathRoot(name); name = name.Substring(root.Length); } } // The Path methods used by ContainsInvalidFileNameChars return argument exception if the filePath contains invalid characters. catch (ArgumentException) { return(true); } Microsoft.VisualStudio.Shell.Url uri = new Microsoft.VisualStudio.Shell.Url(name); // This might be confusing bur Url.IsFile means that the uri represented by the name is either absolut or relative. if (uri.IsFile) { string[] segments = uri.Segments; if (segments != null && segments.Length > 0) { foreach (string segment in segments) { if (IsFilePartInValid(segment)) { return(true); } } // Now the last segment should be specially taken care, since that cannot be all dots or spaces. string lastSegment = segments[segments.Length - 1]; string filePart = Path.GetFileNameWithoutExtension(lastSegment); // if the file is only an extension (.fob) then it's ok, otherwise we need to do the special checks. if (filePart.Length != 0 && (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart))) { return(true); } } } else { // The assumption here is that we got a file name. string filePart = Path.GetFileNameWithoutExtension(name); if (IsFileNameAllGivenCharacter('.', filePart) || IsFileNameAllGivenCharacter(' ', filePart)) { return(true); } return(IsFilePartInValid(name)); } return(false); }
/// <summary> /// Brings up the browse folder dialog. /// </summary> /// <param name="sender">The browse button.</param> /// <param name="e">The <see cref="EventArgs"/> object that contains the event data.</param> private void OnBrowseButtonClick(object sender, EventArgs e) { // initialize the dialog to the current directory (if it exists) bool overridePersistedInitialDirectory = false; string initialDirectory = null; if (Directory.Exists(FullPath)) { initialDirectory = FullPath; overridePersistedInitialDirectory = true; } IntPtr parentWindow = Handle; Guid persistenceSlot = typeof(FileBrowserTextBox).GUID; IVsUIShell2 shell = (IVsUIShell2)Package.GetGlobalService(typeof(SVsUIShell)); // show the dialog string path = shell.GetDirectoryViaBrowseDialog(parentWindow, persistenceSlot, "Select folder", initialDirectory, overridePersistedInitialDirectory); if (path != null) { if (MakeRelative && !string.IsNullOrEmpty(RootFolder)) { string rootFolder = Path.GetFullPath(RootFolder); if (Directory.Exists(rootFolder)) { if (!rootFolder.EndsWith(Path.DirectorySeparatorChar.ToString()) && !rootFolder.EndsWith(Path.AltDirectorySeparatorChar.ToString())) rootFolder = rootFolder + Path.DirectorySeparatorChar; path = new Url(rootFolder).MakeRelative(new Url(path)); } } this.folderTextBox.Text = path; } }
/// <summary> /// Brings up the browse folder dialog. /// </summary> /// <param name="sender">The browse button.</param> /// <param name="e">The <see cref="EventArgs"/> object that contains the event data.</param> private void OnBrowseButtonClick(object sender, EventArgs e) { // initialize the dialog to the current file (if it exists) string fullPath = this.FullPath; if (File.Exists(fullPath)) { this.fileBrowserDialog.InitialDirectory = Path.GetDirectoryName(fullPath); this.fileBrowserDialog.FileName = Path.GetFileName(fullPath); } // show the dialog if (this.fileBrowserDialog.ShowDialog(this) == DialogResult.OK) { fullPath = this.fileBrowserDialog.FileName; string path = fullPath; if (MakeRelative && !string.IsNullOrEmpty(RootFolder)) { string rootFolder = Path.GetFullPath(RootFolder); if (Directory.Exists(rootFolder)) { if (!rootFolder.EndsWith(Path.DirectorySeparatorChar.ToString()) && !rootFolder.EndsWith(Path.AltDirectorySeparatorChar.ToString())) rootFolder = rootFolder + Path.DirectorySeparatorChar; path = new Url(rootFolder).MakeRelative(new Url(fullPath)); } } this.fileTextBox.Text = path; } }