/// <summary> /// if Target is a file, launch explorer to file /// if target is a folder, launch explorer to open said folder /// </summary> /// <param name="Target"></param> static void Explore(string Target) { StringBuilder NewTarget = new StringBuilder(); string ShellCmd = ArgumentExpander.ExpandVars(ExploreCmd); bool FileMode = false; if (File.Exists(Target) == false) { if (Directory.Exists(Target) == false) { MessageBox.Show(string.Format("Error: {0} not found", Target)); } else { FileMode = false; } } else { FileMode = true; } if (FileMode) { NewTarget.Append(Path.GetDirectoryName(Target)); } else { NewTarget.Append(Target); } LaunchExternCommand(ShellCmd, NewTarget.ToString(), false); }
private void ButtonChooseFile_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(TextBoxFilePath.Text)) { openFileDialog1.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } else { if ((File.Exists(TextBoxFilePath.Text) == true) || (Directory.Exists(TextBoxFilePath.Text) == true)) { openFileDialog1.FileName = TextBoxFilePath.Text; } else { if (TextBoxFilePath.Text.Contains("$")) { string Expand; // and try again Expand = ArgumentExpander.ExpandVars(TextBoxFilePath.Text); if ((File.Exists(Expand) == true) || (Directory.Exists(Expand) == true)) { openFileDialog1.FileName = Expand; } else { openFileDialog1.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); } } } } if (Directory.Exists(openFileDialog1.FileName)) { openFileDialog1.InitialDirectory = openFileDialog1.FileName; openFileDialog1.FileName = string.Empty; openFileDialog1.RestoreDirectory = true; } else { if (File.Exists(openFileDialog1.FileName)) { openFileDialog1.InitialDirectory = Path.GetDirectoryName(openFileDialog1.FileName); openFileDialog1.FileName = Path.GetFileName(openFileDialog1.FileName); openFileDialog1.RestoreDirectory = true; } else { openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); openFileDialog1.RestoreDirectory = true; openFileDialog1.FileName = string.Empty; } } openFileDialog1.FileOk += OpenFileDialog1_FileOk; openFileDialog1.ShowDialog(); }
static void LaunchCmd(string Target, bool TryAdmin) { StringBuilder cmd; string CmdArg; string NewTarget; if (string.IsNullOrEmpty(Target)) { return; } if (File.Exists(Target)) { NewTarget = Path.GetDirectoryName(Target); } else { if (Directory.Exists(Target)) { NewTarget = Target; } else { NewTarget = string.Empty; } } if (string.IsNullOrEmpty(NewTarget)) { MessageBox.Show(string.Format("{0} Does not appear to exist. . .", Target)); } else { cmd = new StringBuilder(); cmd.AppendFormat(ArgumentExpander.ExpandVars(LaunchCmdPrompt), NewTarget, string.Empty); CmdArg = string.Format(LaunchCmdPromptArgString, Path.GetDirectoryName(ArgumentExpander.ExpandVars(Target))); if (TryAdmin) { LaunchExternCommand(cmd.ToString(), CmdArg, true, "runas", Target); } else { LaunchExternCommand(cmd.ToString(), CmdArg, true); } } }