private void click_explore_icons_progy(object sender, RoutedEventArgs e) { RunCommand x = new RunCommand { Command = MyImageWorker.myPath }; x.ExecuteCommandSync(); }
// create list to hold extra files private void DoDrop(object sender, DragEventArgs e) { if (draging) { e.Handled = true; return; } draging = true; List<string> errors = new List<string>(); bool usePGB = false; //only one image can be added at a time bool oneImageadded = false; if (commandRepositoryViewSource.View.CurrentItem == null) { e.Handled = true; return; } #region enable statusbar togleEnable(false); //Create a new instance of our ProgressBar Delegate that points // to the ProgressBar's SetValue method. UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(ProgressBarStatus.SetValue); //Stores the value of the ProgressBar double value = 0; #endregion try { // Handle FileDrop data. if (e.Data.GetDataPresent(DataFormats.FileDrop)) { // Check if we have been given only one file, if not kill the user with error message! string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); List<string> newList = cleanUpListAndFindDirectories(files); if (files.Count() > 1) oneImageadded = true; #region enable statusbar if (newList.Count() > 2) { usePGB = true; //Configure the ProgressBar StatusGrid.Visibility = System.Windows.Visibility.Visible; ProgressBarStatus.Minimum = 0; ProgressBarStatus.Maximum = newList.Count(); ProgressbarLabel.Text = "Gettering information of files..."; ProgressBarStatus.Value = 0; } #endregion foreach (string s in newList) { ProgressbarLabel.Text = "Processing file: " + s; System.Windows.Forms.Application.DoEvents(); try { if (System.IO.Path.GetExtension(s).Equals(".lnk", StringComparison.CurrentCultureIgnoreCase)) { try { RunCommand newCmd = RunCommand.CreateCommandFromLnk(s); if (newCmd != null && newCmd.Error.Length == 0) { (commandRepositoryViewSource.View.CurrentItem as CommandRepository).Commands.Add(newCmd); runCommandViewSource.View.MoveCurrentToLast(); runCommandDataGrid.ScrollIntoView(runCommandDataGrid.SelectedItem); DataGridRow dgrow = (DataGridRow)runCommandDataGrid.ItemContainerGenerator.ContainerFromItem(runCommandDataGrid.SelectedItem); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); oneImageadded = true; } } catch (Exception ex) { errors.Add("error in file: " + s + " " + ex.Message + Environment.NewLine); } #endregion } else { #region check if its image //file is not an ink object, check if its an image bool isSuportedExtension = false; string fileExstension = System.IO.Path.GetExtension(s); foreach (string item in RunCommand.imageExt) { if (fileExstension.ToLower().Equals(item, StringComparison.CurrentCultureIgnoreCase)) { isSuportedExtension = true; break; } } #endregion if (!isSuportedExtension)// it is not a supported image, check if we want to make a shortcut out of is --> extensions properties { bool isValidExtension = false; foreach (string sr in Properties.Settings.Default.MakeShortCutExtensions) { if (System.IO.Path.GetExtension(s).Equals(sr, StringComparison.CurrentCultureIgnoreCase)) { isValidExtension = true; break; } } if (isValidExtension) { #region Make it into a shortcut try { // Start fetching new runcommand. RunCommand newCmd = RunCommand.CreateCommandFromstring(s); if (newCmd != null && newCmd.Error.Length == 0) { (commandRepositoryViewSource.View.CurrentItem as CommandRepository).Commands.Add(newCmd); runCommandViewSource.View.MoveCurrentToLast(); runCommandDataGrid.ScrollIntoView(runCommandDataGrid.SelectedItem); DataGridRow dgrow = (DataGridRow)runCommandDataGrid.ItemContainerGenerator.ContainerFromItem(runCommandDataGrid.SelectedItem); dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); oneImageadded = true; } } catch (Exception ex) { errors.Add(s + " failed for shortcut creation: " + ex.Message + Environment.NewLine); } #endregion } } else { #region Add maximum one image if (!oneImageadded)//if more items were provided, only add one image { HitTestResult result = VisualTreeHelper.HitTest(this, e.GetPosition(this)); DependencyObject obj = result.VisualHit; while (VisualTreeHelper.GetParent(obj) != null && !(obj is DataGrid)) { obj = VisualTreeHelper.GetParent(obj); if (obj is DataGridRow) { runCommandDataGrid.SelectedItem = (obj as DataGridRow).Item; break; } } if (runCommandViewSource.View.CurrentItem != null) { (runCommandViewSource.View.CurrentItem as RunCommand).IconLocation = s; } oneImageadded = true; } } #endregion } } catch (Exception ex) { errors.Add("Unhandled error" + ex.Message + Environment.NewLine); } finally { #region Step statusbar if (usePGB) { value += 1; Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value }); } #endregion } } } #region textdrop = only for images // Handle TextDrop data. Texstdrops are only for icons! if (e.Data.GetDataPresent(DataFormats.Text)) { if (runCommandViewSource.View.CurrentItem == null) { HitTestResult result = VisualTreeHelper.HitTest(this, e.GetPosition(this)); DependencyObject obj = result.VisualHit; while (VisualTreeHelper.GetParent(obj) != null && !(obj is DataGrid)) { obj = VisualTreeHelper.GetParent(obj); if (obj is DataGridRow) { runCommandDataGrid.SelectedItem = (obj as DataGridRow).Item; oneImageadded = true; } } } string file = (string)e.Data.GetData(DataFormats.Text); (runCommandDataGrid.SelectedItem as RunCommand).IconLocation = file; } #endregion } catch (Exception ex) { errors.Add(ex.Message + Environment.NewLine + Environment.NewLine); } finally { #region reporting errors if (errors.Count >= 1 && errors.Count < 50) { StringBuilder sb = new StringBuilder(); foreach (string x in errors) sb.Append(x); sendMessage("Drop failer for the item(s): " + Environment.NewLine + Environment.NewLine + sb.ToString(), "Drop error(s)"); } else if (errors.Count > 50) { sendMessage("a large number of files were logged, they will be displayed in a txt file...", "Drop error(s)"); StringBuilder sb = new StringBuilder(); foreach (string x in errors) sb.Append(x); try { string tempPath = System.IO.Path.GetTempPath() + DateTime.Now.ToFileTime() + ".txt"; File.WriteAllText(tempPath, sb.ToString()); RunCommand x = new RunCommand(); x.Type = CommandType.SimpleRunCommand; x.Command = tempPath; x.ExecuteCommandSync(); } catch (Exception ex) { sendMessage("Fatal error!" + Environment.NewLine + ex.Message, "Writing to textfile failed."); } } #endregion #region Step statusbar StatusGrid.Visibility = System.Windows.Visibility.Collapsed; ProgressbarLabel.Text = string.Empty; if (usePGB) { value = 0; /*Update the Value of the ProgressBar: 1) Pass the "updatePbDelegate" delegate that points to the ProgressBar1.SetValue method 2) Set the DispatcherPriority to "Background" 3) Pass an Object() Array containing the property to update (ProgressBar.ValueProperty) and the new value */ Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, value }); } togleEnable(true); #endregion draging = false; e.Handled = true; } }
public static RunCommand CreateCommandFromLnk ( string s ) { ShellLink link = null; try { #region Read out the LNK file //read out shortcut properties link = new ShellLink ( s ); string tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( link. ShortCutFile ) + ".png"; string quickyName = System. IO. Path. GetFileNameWithoutExtension ( s ); //Before adding check if name exists int counter = 0; string dummyName = quickyName; //make sure the name is unique! if ( RepositoryLibrary. ExistsQuicky ( quickyName ) ) { do { quickyName = dummyName + "(" + counter++. ToString ( ) + ")"; } while ( RepositoryLibrary. ExistsQuicky ( quickyName ) ); } labelXx1: try { File. Delete ( tempPath ); //clean file if it already exists } catch ( Exception ) { //File is prob in use.. increment file name with datetimenow tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( link. ShortCutFile ) + DateTime. Now. Millisecond. ToString ( ) + ".png"; goto labelXx1; } //get icon and save it to temp location link. LargeIcon. ToBitmap ( ). Save ( tempPath, System. Drawing. Imaging. ImageFormat. Png ); RunCommand tmp = new RunCommand { IconLocation = tempPath, Command = link. Target, Quicky = quickyName, Arguments = link. Arguments, Desctiption = link. Description }; return tmp; } catch ( Exception ) { return null; throw; } finally { if ( link != null ) link. Dispose ( ); } #endregion }
public static RunCommand CreateCommandFromstring ( string path ) { RunCommand returnCommand = null; try { FileInfo file = new FileInfo ( path ); string tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( path ) + ".png"; string quickyName = System. IO. Path. GetFileNameWithoutExtension ( path ); //Before adding check if name exists int counter = 0; string dummyName = quickyName; //make sure the name is unique! if ( RepositoryLibrary. ExistsQuicky ( quickyName ) ) { do { quickyName = dummyName + "(" + counter++. ToString ( ) + ")"; } while ( RepositoryLibrary. ExistsQuicky ( quickyName ) ); } FileIcon fi = null; try { fi = new FileIcon ( path ); labelXx2: try { File. Delete ( tempPath ); //clean file if it already exists } catch ( Exception ) { //File is prob in use.. increment file name with datetimenow tempPath = System. IO. Path. GetTempPath ( ) + System. IO. Path. GetFileNameWithoutExtension ( path ) + DateTime. Now. Millisecond. ToString ( ) + ".png"; goto labelXx2; } //get icon and save it to temp location fi. ShellIcon. ToBitmap ( ). Save ( tempPath, System. Drawing. Imaging. ImageFormat. Png ); } catch ( Exception ) { //if it did not succeed do nothing, this is only for icon so null it tempPath = ""; } finally { fi. ShellIcon. Dispose ( ); } returnCommand = new RunCommand { IconLocation = tempPath, Command = path, Quicky = quickyName, Arguments = string. Empty, Desctiption = string. Empty }; return returnCommand; } catch ( Exception ) { return returnCommand; throw; } finally { } }
private void txtInput_KeyDown ( object sender, KeyEventArgs e ) { if ( e. Key == Key. Enter ) { Objects. RunCommand x = txtInput. SelectedItem as Objects. RunCommand; if ( x != null ) { try { x. ExecuteCommandSync ( this ); lastUsedCommand = x; this. Close ( ); } catch ( Exception ex ) { MyMessageBox. SendMessage ( ex. Message, x. Desctiption ); } } else { Objects. RunCommand x1 = new Objects. RunCommand { Command = txtInput. Text, Desctiption = "Unknown command", Type = CommandType. SimpleRunCommand }; if ( !string. IsNullOrWhiteSpace ( x1. Command ) ) { try { x1. ExecuteCommandSync ( this ); lastUsedCommand = x1; this. Close ( ); } catch ( Exception ex ) { MyMessageBox. SendMessage ( ex. Message, x1. Desctiption ); } } else try { if ( lastUsedCommand != null ) lastUsedCommand. ExecuteCommandSync ( this ); } catch ( Exception ex ) { MyMessageBox. SendMessage ( ex. Message, lastUsedCommand. Desctiption ); } } } if ( e. Key == Key. Escape ) { if ( string. IsNullOrWhiteSpace ( this. txtInput. Text ) ) { this. Close ( ); } else { imageView. ImageSource = null; this. txtInput. Text = ""; this. txtInput. SelectedIndex = -1; } } }