15
15
namespace ITHit . FileSystem . Samples . Common . Windows
16
16
{
17
17
/// <summary>
18
- /// Commands sent from tray app and comnsole .
18
+ /// Commands sent from tray app and console .
19
19
/// </summary>
20
20
public class Commands
21
21
{
@@ -30,7 +30,7 @@ public class Commands
30
30
public ISyncService RemoteStorageMonitor ;
31
31
32
32
/// <summary>
33
- /// Remote storaage path.
33
+ /// Remote storage path.
34
34
/// </summary>
35
35
private readonly string RemoteStorageRootPath ;
36
36
@@ -39,6 +39,12 @@ public class Commands
39
39
/// </summary>
40
40
private readonly ILog log ;
41
41
42
+ /// <summary>
43
+ /// Initializes a new instance of the <see cref="Commands"/> class.
44
+ /// </summary>
45
+ /// <param name="engine">The engine instance.</param>
46
+ /// <param name="remoteStorageRootPath">The remote storage root path.</param>
47
+ /// <param name="log">The logger instance.</param>
42
48
public Commands ( EngineWindows engine , string remoteStorageRootPath , ILog log )
43
49
{
44
50
this . Engine = engine ;
@@ -47,7 +53,7 @@ public Commands(EngineWindows engine, string remoteStorageRootPath, ILog log)
47
53
}
48
54
49
55
/// <summary>
50
- /// Start/ stop the Engine and all sync services.
56
+ /// Start or stop the Engine and all sync services.
51
57
/// </summary>
52
58
public async Task StartStopEngineAsync ( )
53
59
{
@@ -64,7 +70,7 @@ public async Task StartStopEngineAsync()
64
70
}
65
71
66
72
/// <summary>
67
- /// Start/ stop synchronization service.
73
+ /// Start or stop the synchronization service.
68
74
/// </summary>
69
75
public async Task StartStopSynchronizationAsync ( )
70
76
{
@@ -85,9 +91,12 @@ public async Task StartStopSynchronizationAsync()
85
91
}
86
92
}
87
93
94
+ /// <summary>
95
+ /// Start or stop the remote storage monitor.
96
+ /// </summary>
88
97
public async Task StartStopRemoteStorageMonitorAsync ( )
89
98
{
90
- if ( RemoteStorageMonitor == null )
99
+ if ( RemoteStorageMonitor == null )
91
100
{
92
101
Engine . Logger . LogError ( "Remote storage monitor is null." , Engine . Path ) ;
93
102
return ;
@@ -98,7 +107,6 @@ public async Task StartStopRemoteStorageMonitorAsync()
98
107
if ( Engine . State != EngineState . Running )
99
108
{
100
109
Engine . Logger . LogError ( "Failed to start. The Engine must be running." , Engine . Path ) ;
101
- //Engine.RemoteStorageMonitor.Logger.LogError("Failed to start. The Engine must be running.");
102
110
return ;
103
111
}
104
112
await RemoteStorageMonitor . StartAsync ( ) ;
@@ -110,37 +118,59 @@ public async Task StartStopRemoteStorageMonitorAsync()
110
118
}
111
119
112
120
/// <summary>
113
- /// Opens path with associated application.
121
+ /// Opens the specified path with the associated application.
114
122
/// </summary>
115
- /// <param name="path">Path to the file or folder.</param>
123
+ /// <param name="path">The path to the file or folder.</param>
116
124
public static void Open ( string path )
117
125
{
118
- ProcessStartInfo startInfo = new ProcessStartInfo ( path ) ;
119
- startInfo . UseShellExecute = true ; // Open window only if not opened already.
126
+ ProcessStartInfo startInfo = new ProcessStartInfo ( path )
127
+ {
128
+ UseShellExecute = true // Open window only if not opened already.
129
+ } ;
120
130
using ( Process ufsWinFileManager = Process . Start ( startInfo ) )
121
131
{
122
-
123
132
}
124
133
}
125
134
126
135
/// <summary>
127
- /// Open root user file system folder in Windows Explorer .
136
+ /// Tries to open the specified path .
128
137
/// </summary>
129
- public async Task OpenRootFolderAsync ( )
138
+ /// <param name="path">The path to the file or folder.</param>
139
+ /// <returns>True if the path was opened successfully, otherwise false.</returns>
140
+ public bool TryOpen ( string path )
130
141
{
131
- Open ( Engine . Path ) ;
142
+ return TryOpen ( path , log ) ;
132
143
}
133
144
134
145
/// <summary>
135
- /// Open remote storage .
146
+ /// Tries to open the specified path .
136
147
/// </summary>
137
- public async Task OpenRemoteStorageAsync ( )
148
+ /// <param name="path">The path to the file or folder.</param>
149
+ /// <returns>True if the path was opened successfully, otherwise false.</returns>
150
+ public static bool TryOpen ( string path , ILog ? log = null )
138
151
{
139
- Open ( RemoteStorageRootPath ) ;
152
+ try
153
+ {
154
+ if ( ! string . IsNullOrEmpty ( path ) && ( File . Exists ( path ) || Directory . Exists ( path ) ) )
155
+ {
156
+ Open ( path ) ;
157
+ return true ;
158
+ }
159
+ else
160
+ {
161
+ log ? . Warn ( $ "The path { path } does not exist.") ;
162
+ }
163
+ }
164
+ catch ( Exception ex )
165
+ {
166
+ log ? . Error ( $ "Failed to open { path } .", ex ) ;
167
+ }
168
+
169
+ return false ;
140
170
}
141
171
142
172
/// <summary>
143
- /// Opens support portal.
173
+ /// Opens the support portal.
144
174
/// </summary>
145
175
public static async Task OpenSupportPortalAsync ( )
146
176
{
@@ -155,11 +185,11 @@ public async Task EngineExitAsync()
155
185
await StopEngineAsync ( ) ;
156
186
log . Info ( $ "\n \n { RemoteStorageRootPath } ") ;
157
187
log . Info ( "\n All downloaded file / folder placeholders remain in file system. Restart the application to continue managing files." ) ;
158
- log . Info ( "\n You can edit documents when the app is not running and than start the app to sync all changes to the remote storage.\n " ) ;
188
+ log . Info ( "\n You can edit documents when the app is not running and then start the app to sync all changes to the remote storage.\n " ) ;
159
189
}
160
190
161
191
/// <summary>
162
- /// Stop the Engine and all sync services.
192
+ /// Stops the Engine and all sync services.
163
193
/// </summary>
164
194
public async Task StopEngineAsync ( )
165
195
{
@@ -173,31 +203,27 @@ public async Task StopEngineAsync()
173
203
/// <summary>
174
204
/// Opens Windows File Manager with both remote storage and user file system for testing.
175
205
/// </summary>
176
- /// <param name="openRemoteStorage">True if the Remote Storage must be opened. False - otherwise.</param>
206
+ /// <param name="openRemoteStorage">True if the Remote Storage must be opened. False otherwise.</param>
177
207
/// <param name="engineIndex">Index used to position Windows Explorer window to show this user file system.</param>
178
- /// <param name="totalEngines">Total number of Engined that will be mounted by this app.</param>
208
+ /// <param name="totalEngines">Total number of Engines that will be mounted by this app.</param>
209
+ /// <param name="userFileSystemWindowName">Name of the user file system window.</param>
210
+ /// <param name="cancellationToken">Cancellation token.</param>
179
211
/// <remarks>This method is provided solely for the development and testing convenience.</remarks>
180
212
public void ShowTestEnvironment ( string userFileSystemWindowName , bool openRemoteStorage = true , CancellationToken cancellationToken = default , int engineIndex = 0 , int totalEngines = 1 )
181
213
{
182
- int numWindowsPerEngine = 2 ; //openRemoteStorage ? 2 : 1; // Each engine shows 2 windows - remote storage and UFS.
183
- int horizintalIndex = engineIndex * numWindowsPerEngine ;
214
+ int numWindowsPerEngine = 2 ; // Each engine shows 2 windows - remote storage and UFS.
215
+ int horizontalIndex = engineIndex * numWindowsPerEngine ;
184
216
int totalWindows = totalEngines * numWindowsPerEngine ;
185
217
186
218
// Open remote storage.
187
219
if ( openRemoteStorage )
188
220
{
189
- Commands . Open ( RemoteStorageRootPath ) ;
190
- //string rsWindowName = Path.GetFileName(RemoteStorageRootPath.TrimEnd('\\'));
191
- //IntPtr hWndRemoteStorage = WindowManager.FindWindow(rsWindowName, cancellationToken);
192
- //WindowManager.PositionFileSystemWindow(hWndRemoteStorage, horizintalIndex, totalWindows);
221
+ TryOpen ( RemoteStorageRootPath ) ;
193
222
}
194
223
195
224
// Open Windows File Manager with user file system.
196
- Commands . Open ( Engine . Path ) ;
197
- //IntPtr hWndUserFileSystem = WindowManager.FindWindow(userFileSystemWindowName, cancellationToken);
198
- //WindowManager.PositionFileSystemWindow(hWndUserFileSystem, horizintalIndex + 1, totalWindows);
225
+ TryOpen ( Engine . Path ) ;
199
226
}
200
-
201
227
#endif
202
228
}
203
229
}
0 commit comments