1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . ComponentModel ;
4+ using System . Data ;
5+ using System . Diagnostics ;
6+ using System . Drawing ;
7+ using System . IO ;
8+ using System . Linq ;
9+ using System . Text ;
10+ using System . Threading . Tasks ;
11+ using System . Windows . Forms ;
12+
13+ namespace VDD_Control
14+ {
15+ public partial class CommunityScriptsForm : Form
16+ {
17+ private const string SCRIPTS_FOLDER = "Community Scripts" ;
18+ private ListBox scriptListBox ;
19+ private Label noScriptsLabel ;
20+
21+ public CommunityScriptsForm ( )
22+ {
23+ InitializeComponents ( ) ;
24+ LoadScripts ( ) ;
25+ }
26+
27+ private void InitializeComponents ( )
28+ {
29+ // Configure form
30+ this . Text = "Community Scripts" ;
31+ this . Size = new Size ( 500 , 400 ) ;
32+ this . FormBorderStyle = FormBorderStyle . FixedDialog ;
33+ this . MaximizeBox = false ;
34+ this . MinimizeBox = false ;
35+ this . StartPosition = FormStartPosition . CenterParent ;
36+ this . BackColor = Color . FromArgb ( 32 , 34 , 37 ) ;
37+ this . ForeColor = Color . White ;
38+
39+ // Create script list box
40+ scriptListBox = new ListBox
41+ {
42+ Dock = DockStyle . Fill ,
43+ BackColor = Color . FromArgb ( 45 , 47 , 49 ) ,
44+ ForeColor = Color . White ,
45+ BorderStyle = BorderStyle . FixedSingle
46+ } ;
47+ scriptListBox . DoubleClick += ScriptListBox_DoubleClick ;
48+
49+ // Create no scripts label
50+ noScriptsLabel = new Label
51+ {
52+ Text = "No scripts found in the Community Scripts folder." ,
53+ TextAlign = ContentAlignment . MiddleCenter ,
54+ Dock = DockStyle . Fill ,
55+ ForeColor = Color . White ,
56+ BackColor = Color . FromArgb ( 45 , 47 , 49 ) ,
57+ Visible = false
58+ } ;
59+
60+ // Add controls to form
61+ this . Controls . Add ( scriptListBox ) ;
62+ this . Controls . Add ( noScriptsLabel ) ;
63+ }
64+
65+ private void LoadScripts ( )
66+ {
67+ // Make sure the scripts directory exists
68+ string scriptsDirectory = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , SCRIPTS_FOLDER ) ;
69+
70+ // Create the directory if it doesn't exist
71+ if ( ! Directory . Exists ( scriptsDirectory ) )
72+ {
73+ Directory . CreateDirectory ( scriptsDirectory ) ;
74+ }
75+
76+ // Find script files
77+ string [ ] scriptFiles = Directory . GetFiles ( scriptsDirectory , "*.*" )
78+ . Where ( file =>
79+ file . EndsWith ( ".cmd" , StringComparison . OrdinalIgnoreCase ) ||
80+ file . EndsWith ( ".bat" , StringComparison . OrdinalIgnoreCase ) ||
81+ file . EndsWith ( ".ps1" , StringComparison . OrdinalIgnoreCase ) ||
82+ file . EndsWith ( ".exe" , StringComparison . OrdinalIgnoreCase ) )
83+ . ToArray ( ) ;
84+
85+ // Clear the list
86+ scriptListBox . Items . Clear ( ) ;
87+
88+ // If no scripts found, show the label
89+ if ( scriptFiles . Length == 0 )
90+ {
91+ scriptListBox . Visible = false ;
92+ noScriptsLabel . Visible = true ;
93+ return ;
94+ }
95+
96+ // Otherwise, hide the label and populate the list
97+ scriptListBox . Visible = true ;
98+ noScriptsLabel . Visible = false ;
99+
100+ // Add scripts to list
101+ foreach ( string scriptFile in scriptFiles )
102+ {
103+ scriptListBox . Items . Add ( Path . GetFileName ( scriptFile ) ) ;
104+ }
105+ }
106+
107+ private void ScriptListBox_DoubleClick ( object sender , EventArgs e )
108+ {
109+ if ( scriptListBox . SelectedItem == null )
110+ return ;
111+
112+ string selectedScript = scriptListBox . SelectedItem . ToString ( ) ;
113+ string scriptPath = Path . Combine (
114+ AppDomain . CurrentDomain . BaseDirectory ,
115+ SCRIPTS_FOLDER ,
116+ selectedScript ) ;
117+
118+ // Execute the script based on its extension
119+ try
120+ {
121+ if ( selectedScript . EndsWith ( ".ps1" , StringComparison . OrdinalIgnoreCase ) )
122+ {
123+ // PowerShell script
124+ Process process = new Process
125+ {
126+ StartInfo = new ProcessStartInfo
127+ {
128+ FileName = "powershell.exe" ,
129+ Arguments = $ "-ExecutionPolicy Bypass -File \" { scriptPath } \" ",
130+ UseShellExecute = false ,
131+ CreateNoWindow = false
132+ }
133+ } ;
134+ process . Start ( ) ;
135+ }
136+ else if ( selectedScript . EndsWith ( ".exe" , StringComparison . OrdinalIgnoreCase ) )
137+ {
138+ // Executable
139+ Process . Start ( scriptPath ) ;
140+ }
141+ else
142+ {
143+ // Batch or CMD file
144+ Process process = new Process
145+ {
146+ StartInfo = new ProcessStartInfo
147+ {
148+ FileName = "cmd.exe" ,
149+ Arguments = $ "/c \" { scriptPath } \" ",
150+ UseShellExecute = false ,
151+ CreateNoWindow = false
152+ }
153+ } ;
154+ process . Start ( ) ;
155+ }
156+ }
157+ catch ( Exception ex )
158+ {
159+ MessageBox . Show ( $ "Error executing script: { ex . Message } ", "Error" ,
160+ MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
161+ }
162+ }
163+
164+ // Method to refresh the script list (can be called externally)
165+ public void RefreshScripts ( )
166+ {
167+ LoadScripts ( ) ;
168+ }
169+ }
170+ }
0 commit comments