WIP: load and save session
This commit is contained in:
		
							parent
							
								
									e0c118cb0b
								
							
						
					
					
						commit
						4416884811
					
				
							
								
								
									
										30
									
								
								MainForm.Designer.cs
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										30
									
								
								MainForm.Designer.cs
									
									
									
										generated
									
									
									
								
							| @ -64,6 +64,8 @@ namespace RandomFileRunner | ||||
|             this.button3 = new RyzStudio.Windows.ThemedForms.TMenuButton(); | ||||
|             this.memoBox1 = new RyzStudio.Windows.ThemedForms.TMemoBox(); | ||||
|             this.button5 = new RyzStudio.Windows.ThemedForms.TButton(); | ||||
|             this.openFileDialog2 = new System.Windows.Forms.OpenFileDialog(); | ||||
|             this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); | ||||
|             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); | ||||
|             this.menuStrip1.SuspendLayout(); | ||||
|             this.contextMenuStrip1.SuspendLayout(); | ||||
| @ -144,7 +146,7 @@ namespace RandomFileRunner | ||||
|             //  | ||||
|             this.newToolStripMenuItem.Name = "newToolStripMenuItem"; | ||||
|             this.newToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.N))); | ||||
|             this.newToolStripMenuItem.Size = new System.Drawing.Size(180, 22); | ||||
|             this.newToolStripMenuItem.Size = new System.Drawing.Size(146, 22); | ||||
|             this.newToolStripMenuItem.Text = "&New"; | ||||
|             this.newToolStripMenuItem.Click += new System.EventHandler(this.newToolStripMenuItem_Click); | ||||
|             //  | ||||
| @ -152,7 +154,7 @@ namespace RandomFileRunner | ||||
|             //  | ||||
|             this.openToolStripMenuItem.Name = "openToolStripMenuItem"; | ||||
|             this.openToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.O))); | ||||
|             this.openToolStripMenuItem.Size = new System.Drawing.Size(180, 22); | ||||
|             this.openToolStripMenuItem.Size = new System.Drawing.Size(146, 22); | ||||
|             this.openToolStripMenuItem.Text = "&Open"; | ||||
|             this.openToolStripMenuItem.Click += new System.EventHandler(this.openToolStripMenuItem_Click); | ||||
|             //  | ||||
| @ -164,7 +166,7 @@ namespace RandomFileRunner | ||||
|             // saveAsToolStripMenuItem | ||||
|             //  | ||||
|             this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem"; | ||||
|             this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); | ||||
|             this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(146, 22); | ||||
|             this.saveAsToolStripMenuItem.Text = "Save &As..."; | ||||
|             this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click); | ||||
|             //  | ||||
| @ -176,7 +178,7 @@ namespace RandomFileRunner | ||||
|             // exitToolStripMenuItem2 | ||||
|             //  | ||||
|             this.exitToolStripMenuItem2.Name = "exitToolStripMenuItem2"; | ||||
|             this.exitToolStripMenuItem2.Size = new System.Drawing.Size(180, 22); | ||||
|             this.exitToolStripMenuItem2.Size = new System.Drawing.Size(146, 22); | ||||
|             this.exitToolStripMenuItem2.Text = "E&xit"; | ||||
|             this.exitToolStripMenuItem2.Click += new System.EventHandler(this.exitToolStripMenuItem2_Click); | ||||
|             //  | ||||
| @ -192,7 +194,7 @@ namespace RandomFileRunner | ||||
|             //  | ||||
|             this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem"; | ||||
|             this.optionsToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.F12))); | ||||
|             this.optionsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); | ||||
|             this.optionsToolStripMenuItem.Size = new System.Drawing.Size(168, 22); | ||||
|             this.optionsToolStripMenuItem.Text = "&Options"; | ||||
|             this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click); | ||||
|             //  | ||||
| @ -210,19 +212,19 @@ namespace RandomFileRunner | ||||
|             //  | ||||
|             this.viewHelpToolStripMenuItem1.Name = "viewHelpToolStripMenuItem1"; | ||||
|             this.viewHelpToolStripMenuItem1.ShortcutKeys = System.Windows.Forms.Keys.F1; | ||||
|             this.viewHelpToolStripMenuItem1.Size = new System.Drawing.Size(180, 22); | ||||
|             this.viewHelpToolStripMenuItem1.Size = new System.Drawing.Size(146, 22); | ||||
|             this.viewHelpToolStripMenuItem1.Text = "&View Help"; | ||||
|             this.viewHelpToolStripMenuItem1.Click += new System.EventHandler(this.viewHelpToolStripMenuItem1_Click); | ||||
|             //  | ||||
|             // toolStripMenuItem16 | ||||
|             //  | ||||
|             this.toolStripMenuItem16.Name = "toolStripMenuItem16"; | ||||
|             this.toolStripMenuItem16.Size = new System.Drawing.Size(177, 6); | ||||
|             this.toolStripMenuItem16.Size = new System.Drawing.Size(143, 6); | ||||
|             //  | ||||
|             // aboutToolStripMenuItem1 | ||||
|             //  | ||||
|             this.aboutToolStripMenuItem1.Name = "aboutToolStripMenuItem1"; | ||||
|             this.aboutToolStripMenuItem1.Size = new System.Drawing.Size(180, 22); | ||||
|             this.aboutToolStripMenuItem1.Size = new System.Drawing.Size(146, 22); | ||||
|             this.aboutToolStripMenuItem1.Text = "&About"; | ||||
|             this.aboutToolStripMenuItem1.Click += new System.EventHandler(this.aboutToolStripMenuItem1_Click); | ||||
|             //  | ||||
| @ -395,6 +397,16 @@ namespace RandomFileRunner | ||||
|             this.button5.TabIndex = 49; | ||||
|             this.button5.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button5_MouseClick); | ||||
|             //  | ||||
|             // openFileDialog2 | ||||
|             //  | ||||
|             this.openFileDialog2.DefaultExt = "jsonfig"; | ||||
|             this.openFileDialog2.Filter = "Session files (*.jsonfig)|*.jsonfig"; | ||||
|             //  | ||||
|             // saveFileDialog1 | ||||
|             //  | ||||
|             this.saveFileDialog1.DefaultExt = "jsonfig"; | ||||
|             this.saveFileDialog1.Filter = "Session files (*.jsonfig)|*.jsonfig"; | ||||
|             //  | ||||
|             // MainForm | ||||
|             //  | ||||
|             this.AllowDrop = true; | ||||
| @ -462,6 +474,8 @@ namespace RandomFileRunner | ||||
|         private RyzStudio.Windows.ThemedForms.TClearableTextBox textBox1; | ||||
|         private RyzStudio.Windows.ThemedForms.TMenuButton button3; | ||||
|         private RyzStudio.Windows.ThemedForms.TButton button5; | ||||
|         private System.Windows.Forms.OpenFileDialog openFileDialog2; | ||||
|         private System.Windows.Forms.SaveFileDialog saveFileDialog1; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										134
									
								
								MainForm.cs
									
									
									
									
									
								
							
							
						
						
									
										134
									
								
								MainForm.cs
									
									
									
									
									
								
							| @ -7,6 +7,7 @@ using System.Diagnostics; | ||||
| using System.Drawing; | ||||
| using System.IO; | ||||
| using System.Runtime.InteropServices; | ||||
| using System.Text.Json; | ||||
| using System.Threading.Tasks; | ||||
| using System.Windows.Forms; | ||||
| 
 | ||||
| @ -143,11 +144,7 @@ namespace RandomFileRunner | ||||
| 
 | ||||
|                 if (this.CurrentSession.ClosePrevOnNext) CloseCurrentProcess(currentProcess); | ||||
| 
 | ||||
|                 foundFiles = new List<string>(); | ||||
|                 currentProcess = null; | ||||
|                 textBox1.Text = "*.*"; | ||||
|                 ThreadControl.SetText(label2, "0"); | ||||
|                 memoBox1.Text = string.Empty; | ||||
|                 ClearSession(); | ||||
|             }); | ||||
|         } | ||||
| 
 | ||||
| @ -156,9 +153,14 @@ namespace RandomFileRunner | ||||
|         /// </summary> | ||||
|         /// <param name="sender"></param> | ||||
|         /// <param name="e"></param> | ||||
|         private void openToolStripMenuItem_Click(object sender, EventArgs e) | ||||
|         private async void openToolStripMenuItem_Click(object sender, EventArgs e) | ||||
|         { | ||||
|             if (this.IsBusy) return; | ||||
| 
 | ||||
|             if (openFileDialog2.ShowDialog() == DialogResult.OK) | ||||
|             { | ||||
|                 await LoadSessionFile(openFileDialog2.FileName); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
| @ -166,9 +168,14 @@ namespace RandomFileRunner | ||||
|         /// </summary> | ||||
|         /// <param name="sender"></param> | ||||
|         /// <param name="e"></param> | ||||
|         private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) | ||||
|         private async void saveAsToolStripMenuItem_Click(object sender, EventArgs e) | ||||
|         { | ||||
|             if (this.IsBusy) return; | ||||
| 
 | ||||
|             if (saveFileDialog1.ShowDialog() == DialogResult.OK) | ||||
|             { | ||||
|                 await SaveSessionFile(saveFileDialog1.FileName); | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         /// <summary> | ||||
| @ -493,6 +500,119 @@ namespace RandomFileRunner | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         private void ClearSession() | ||||
|         { | ||||
|             foundFiles = new List<string>(); | ||||
|             currentProcess = null; | ||||
|             textBox1.Text = "*.*"; | ||||
|             ThreadControl.SetText(label2, "0"); | ||||
|             memoBox1.Text = string.Empty; | ||||
|         } | ||||
| 
 | ||||
|         protected async Task LoadSessionFile(string filename) | ||||
|         { | ||||
|             await Task.Run(() => | ||||
|             { | ||||
|                 if (string.IsNullOrWhiteSpace(filename)) return; | ||||
|                 if (!File.Exists(filename)) return; | ||||
| 
 | ||||
|                 string sourceCode = null; | ||||
| 
 | ||||
|                 try | ||||
|                 { | ||||
|                     sourceCode = File.ReadAllText(filename); | ||||
|                 } | ||||
|                 catch (Exception exc) | ||||
|                 { | ||||
|                     MessageBox.Show(exc.Message, "Load session"); | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 if (string.IsNullOrWhiteSpace(sourceCode)) | ||||
|                 { | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 // load options | ||||
|                 var options = new JsonSerializerOptions(); | ||||
|                 //options.Converters.Add(new JsonPointConverter()); | ||||
|                 //options.Converters.Add(new JsonSizeConverter()); | ||||
| 
 | ||||
|                 try | ||||
|                 { | ||||
|                     this.CurrentSession = JsonSerializer.Deserialize<AppSession>(sourceCode, options); | ||||
|                 } | ||||
|                 catch (Exception exc) | ||||
|                 { | ||||
|                     MessageBox.Show("Unable to read session", "Load session"); | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 if (this.CurrentSession == null) this.CurrentSession = new AppSession(); | ||||
| 
 | ||||
|                 ClearSession(); | ||||
| 
 | ||||
|                 textBox1.Text = (string.IsNullOrWhiteSpace(this.CurrentSession.SearchFilePattern) ? "*" : this.CurrentSession.SearchFilePattern?.Trim()); | ||||
| 
 | ||||
|                 if (this.CurrentSession.SearchItems != null) | ||||
|                 { | ||||
|                     foreach (string item in this.CurrentSession.SearchItems) | ||||
|                     { | ||||
|                         AddSearchItem(item); | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 // hotkey | ||||
|                 InvalidateHotKey(); | ||||
|             }); | ||||
|         } | ||||
| 
 | ||||
|         protected async Task SaveSessionFile(string filename) | ||||
|         { | ||||
|             await Task.Run(() => | ||||
|             { | ||||
|                 if (string.IsNullOrWhiteSpace(filename)) return; | ||||
| 
 | ||||
|                 if (this.CurrentSession == null) this.CurrentSession = new AppSession(); | ||||
|                 this.CurrentSession.SearchFilePattern = textBox1.Text; | ||||
|                 this.CurrentSession.SearchItems = new List<string>(); | ||||
| 
 | ||||
|                 if (!string.IsNullOrWhiteSpace(memoBox1.Text)) | ||||
|                 { | ||||
|                     foreach (string item in memoBox1.Text?.Trim().Split('\n')) | ||||
|                     { | ||||
|                         if (string.IsNullOrWhiteSpace(item)) | ||||
|                         { | ||||
|                             continue; | ||||
|                         } | ||||
| 
 | ||||
|                         this.CurrentSession.SearchItems.Add(item?.Trim()); | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 string sourceCode = null; | ||||
| 
 | ||||
|                 try | ||||
|                 { | ||||
|                     sourceCode = JsonSerializer.Serialize(this.CurrentSession); | ||||
|                 } | ||||
|                 catch (Exception) | ||||
|                 { | ||||
|                     MessageBox.Show("Unable to write session", "Save session"); | ||||
|                     return; | ||||
|                 } | ||||
| 
 | ||||
|                 try | ||||
|                 { | ||||
|                     File.WriteAllText(filename, sourceCode); | ||||
|                 } | ||||
|                 catch (Exception exc) | ||||
|                 { | ||||
|                     MessageBox.Show(exc.Message, "Save session"); | ||||
|                     return; | ||||
|                 } | ||||
|             }); | ||||
|         } | ||||
| 
 | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -92,6 +92,15 @@ | ||||
|         Ri7JQ2erDp3mBs7w6jaFZht74MaYGwmLbkeRGexGAAAAAElFTkSuQmCC | ||||
| </value> | ||||
|   </data> | ||||
|   <metadata name="openFileDialog2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | ||||
|     <value>1155, 17</value> | ||||
|   </metadata> | ||||
|   <metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> | ||||
|     <value>1295, 17</value> | ||||
|   </metadata> | ||||
|   <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> | ||||
|     <value>108</value> | ||||
|   </metadata> | ||||
|   <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> | ||||
|     <value> | ||||
|         AAABAAQAMDAAAAEAIACoJQAARgAAACAgAAABACAAqBAAAO4lAAAYGAAAAQAgAIgJAACWNgAAEBAAAAEA | ||||
|  | ||||
| @ -1,4 +1,6 @@ | ||||
| namespace RandomFileRunner | ||||
| using System.Collections.Generic; | ||||
| 
 | ||||
| namespace RandomFileRunner | ||||
| { | ||||
|     public class AppSession | ||||
|     { | ||||
| @ -15,6 +17,10 @@ | ||||
|         } | ||||
| 
 | ||||
| 
 | ||||
|         public string SearchFilePattern { get; set; } = "*"; | ||||
| 
 | ||||
|         public List<string> SearchItems { get; set; } = new List<string>(); | ||||
| 
 | ||||
|         public bool SearchTopDirectoryOnly { get; set; } = false; | ||||
| 
 | ||||
|         public bool ClosePrevOnNext { get; set; } = false; | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Ray
						Ray