This repository was archived by the owner on Nov 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtils.cs
590 lines (505 loc) · 21 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
#region Copyright Syncfusion Inc. 2001 - 2022
// Copyright Syncfusion Inc. 2001 - 2022. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Diagnostics;
using Microsoft.Win32;
using System.Globalization;
using System.IO;
using MVCSampleBrowser.Helpers;
using System.Web;
/// <summary>
/// Summary description for Utils
/// </summary>
///
namespace MVCSampleBrowser.Utils
{
#region Class StartDevelopmentWebServer
public class CassiniWebServer
{
const string iisExpressExename = "iisexpress.exe";
const string webServerExeName = "WebDev.WebServer.EXE";
const string Netfx20PathRegKeyName = "Software\\Microsoft\\.NETFramework";
const string Netfx20PathRegValueName = "InstallRoot";
const string SharedFolderPathRegKeyName = "Software\\Microsoft\\Shared Tools";
const string SharedFolderPathRegValueName = "SharedFilesDir";
#region Public Methods
public static void StartVersion20WebServer(string path, string vDirName, out string port)
{
if (!Directory.Exists(path))
throw new Exception("Unable to locate sample directory.");
string WebserverPath = string.Format("{0}\\{1}", GetMSBuildPath(), webServerExeName);
if (File.Exists(WebserverPath))
{
StartWebServer(WebserverPath, path, vDirName, out port);
}
else
throw new NullReferenceException("Unable to locate WebDev.WebServer.EXE");
}
public static void StartVersion3xWebServer(string path, string vDirName, out string port)
{
string WebserverPath = string.Format("{0}\\{1}", GetSharedFolderPath(), webServerExeName);
if (File.Exists(WebserverPath))
{
StartWebServer(WebserverPath, path, vDirName, out port);
}
else
throw new NullReferenceException("Unable to locate WebDev.WebServer.EXE");
}
public static void StartVersion4xWebServer(string path, string vDirName, out string port)
{
string webServerDir = string.Empty;
webServerDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Common Files\Microsoft Shared\DevServer\10.0";
if (webServerDir == null)
{
webServerDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Microsoft Shared\DevServer\10.0";
}
string WebserverPath = string.Format("{0}\\{1}", webServerDir, "WebDev.WebServer40.EXE");
if (File.Exists(WebserverPath))
{
StartWebServer(WebserverPath, path, vDirName, out port);
}
else
throw new NullReferenceException("Unable to locate WebDev.WebServer40.EXE");
}
public static void StartVersion45xWebServer(string path, string vDirName, out string port)
{
string webServerDir = string.Empty;
webServerDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Common Files\Microsoft Shared\DevServer\11.0";
if (webServerDir == null)
{
webServerDir = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + @"\Microsoft Shared\DevServer\11.0";
}
string WebserverPath = string.Format("{0}\\{1}", webServerDir, "WebDev.WebServer40.EXE");
if (MvcLaunchPageExt.RequestedServer() != ServerMode.IISExpress && File.Exists(WebserverPath))
{
StartWebServer(WebserverPath, path, vDirName, out port);
}
else
{
string iisExpressPath = string.Format("{0}\\{1}", GetIISExpressPath(), iisExpressExename);
if (File.Exists(iisExpressPath))
{
StartIISExpress(iisExpressPath, path, vDirName, out port);
}
else
{
throw new NullReferenceException("Unable to locate WebDev.WebServer.EXE");
}
}
}
#endregion
#region Private methods
private static void StartWebServer(string WebserverPath, string samplepath, string vDirName, out string port)
{
string commandArgs = string.Empty;
Random r = new Random();
port = r.Next(1024, 9000).ToString();
//grab the original path
commandArgs += " /path:\"" + samplepath + "\"";
commandArgs += " /port:";
commandArgs += port;
commandArgs += " /vpath:/" + vDirName;
Process mDOSProcess = new Process();
mDOSProcess.StartInfo.FileName = WebserverPath;
mDOSProcess.StartInfo.Arguments = commandArgs;
//if you dont want to see the dos black screen
mDOSProcess.StartInfo.CreateNoWindow = true;
mDOSProcess.StartInfo.UseShellExecute = false;
//now start the process
mDOSProcess.Start();
}
private static void StartIISExpress(string iisExpressPath, string samplepath, string vDirName, out string port)
{
string commandArgs = string.Empty;
Random r = new Random();
port = r.Next(1024, 9000).ToString();
//grab the original path
commandArgs += " /path:\"" + samplepath + "\"";
commandArgs += " /port:";
commandArgs += port;
//commandArgs += " /vpath:/" + vDirName;
Process mDOSProcess = new Process();
mDOSProcess.StartInfo.FileName = iisExpressPath;
mDOSProcess.StartInfo.Arguments = commandArgs;
//////if you dont want to see the dos black screen
mDOSProcess.StartInfo.CreateNoWindow = true;
mDOSProcess.StartInfo.UseShellExecute = false;
//now start the process
mDOSProcess.Start();
}
private static string GetMSBuildPath()
{
string regValue = string.Empty;
if (FrameworkVersionDetection.GetRegistryValue(RegistryHive.LocalMachine, Netfx20PathRegKeyName, Netfx20PathRegValueName, RegistryValueKind.String, out regValue))
{
Version curVersion = FrameworkVersionDetection.GetExactVersion(FrameworkVersion.Fx20);
return string.Format("{0}{1}", regValue, string.Format("v{0}.{1}.{2}", curVersion.Major, curVersion.Minor, curVersion.Build));
}
else
throw new NullReferenceException("Unable to find .NET Framework Root path.");
}
private static string GetIISExpressPath()
{
string programFilesFolder = Environment.GetEnvironmentVariable("PROGRAMFILES");
return programFilesFolder + @"\IIS Express";
}
private static string GetSharedFolderPath()
{
string regValue = string.Empty;
if (FrameworkVersionDetection.GetRegistryValue(RegistryHive.LocalMachine, SharedFolderPathRegKeyName, SharedFolderPathRegValueName, RegistryValueKind.String, out regValue))
{
return string.Format("{0}{1}", regValue, "DevServer\\9.0");
}
else
throw new NullReferenceException("Unable to find Windows Shared folder path.");
}
#endregion
}
#endregion
#region enum FrameworkVersion
/// <summary>
/// Specifies the .NET Framework versions
/// </summary>
public enum FrameworkVersion
{
/// <summary>
/// .NET Framework 1.0
/// </summary>
Fx10,
/// <summary>
/// .NET Framework 1.1
/// </summary>
Fx11,
/// <summary>
/// .NET Framework 2.0
/// </summary>
Fx20,
/// <summary>
/// .NET Framework 3.0
/// </summary>
Fx30,
/// <summary>
/// .NET Framework 3.5 (Orcas)
/// </summary>
Fx35,
/// <summary>
/// .NET Framework 3.5 (SP1)
/// </summary>
Fx35Sp1,
}
#endregion
#region class FrameworkVersionDetection
/// <summary>
/// Provides support for determining if a specific version of the .NET
/// Framework runtime is installed and the service pack level for the
/// runtime version.
/// </summary>
public static class FrameworkVersionDetection
{
#region class-wide fields
const string Netfx10RegKeyName = "Software\\Microsoft\\.NETFramework\\Policy\\v1.0";
const string Netfx10RegKeyValue = "3705";
const string Netfx10SPxMSIRegKeyName = "Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}";
const string Netfx10SPxOCMRegKeyName = "Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}";
const string Netfx10SPxRegValueName = "Version";
const string Netfx11RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322";
const string Netfx20RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727";
const string Netfx30RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup";
const string Netfx35RegKeyName = "Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5";
const string Netfx11PlusRegValueName = "Install";
const string Netfx30PlusRegValueName = "InstallSuccess";
const string Netfx11PlusSPxRegValueName = "SP";
const string Netfx35PlusSPxRegValueName = "SP";
const string Netfx20PlusBuildRegValueName = "Increment";
const string Netfx30PlusVersionRegValueName = "Version";
const string Netfx35PlusBuildRegValueName = "Build";
const string Netfx30PlusWCFRegKeyName = Netfx30RegKeyName + "\\Windows Communication Foundation";
const string Netfx30PlusWPFRegKeyName = Netfx30RegKeyName + "\\Windows Presentation Foundation";
const string Netfx30PlusWFRegKeyName = Netfx30RegKeyName + "\\Windows Workflow Foundation";
const string Netfx30PlusWFPlusVersionRegValueName = "FileVersion";
const string CardSpaceServicesRegKeyName = "System\\CurrentControlSet\\Services\\idsvc";
const string CardSpaceServicesPlusImagePathRegName = "ImagePath";
#endregion
#region private and internal properties and methods
#region methods
#region GetRegistryValue
public static bool GetRegistryValue<T>(RegistryHive hive, string key, string value, RegistryValueKind kind, out T data)
{
bool success = false;
data = default(T);
using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty))
{
if (baseKey != null)
{
using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree))
{
if (registryKey != null)
{
// If the key was opened, try to retrieve the value.
RegistryValueKind kindFound = registryKey.GetValueKind(value);
if (kindFound == kind)
{
object regValue = registryKey.GetValue(value, null);
if (regValue != null)
{
data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
success = true;
}
}
}
}
}
}
return success;
}
#endregion
#region GetNetfxExactVersion functions
#region GetNetfx20ExactVersion
private static Version GetNetfx20ExactVersion()
{
string regValue = String.Empty;
// We can only get -1 if the .NET Framework is not
// installed or there was some kind of error retrieving
// the data from the registry
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, Netfx20PlusBuildRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
// In the strict sense, we are cheating here, but the registry key name itself
// contains the version number.
string[] versionTokens = Netfx20RegKeyName.Split(new string[] { "NDP\\v" }, StringSplitOptions.None);
if (versionTokens.Length == 2)
{
string[] tokens = versionTokens[1].Split('.');
if (tokens.Length == 3)
{
fxVersion = new Version(Convert.ToInt32(tokens[0], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[1], NumberFormatInfo.InvariantInfo), Convert.ToInt32(tokens[2], NumberFormatInfo.InvariantInfo), Convert.ToInt32(regValue, NumberFormatInfo.InvariantInfo));
}
}
}
}
return fxVersion;
}
#endregion
#region GetNetfx30ExactVersion
private static Version GetNetfx30ExactVersion()
{
string regValue = String.Empty;
// We can only get the default version if the .NET Framework
// is not installed or there was some kind of error retrieving
// the data from the registry
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30RegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#region GetNetfx35ExactVersion
private static Version GetNetfx35ExactVersion()
{
string regValue = String.Empty;
// We can only get the default version if the .NET Framework
// is not installed or there was some kind of error retrieving
// the data from the registry
Version fxVersion = new Version();
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx30PlusVersionRegValueName, RegistryValueKind.String, out regValue))
{
if (!String.IsNullOrEmpty(regValue))
{
fxVersion = new Version(regValue);
}
}
return fxVersion;
}
#endregion
#endregion
#region IsNetfxInstalled functions
#region IsNetfx20Installed
private static bool IsNetfx20Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx20RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
public static bool IsNetfx35SP1Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx35PlusSPxRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx30Installed
private static bool IsNetfx30Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx30RegKeyName, Netfx30PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#region IsNetfx35Installed
private static bool IsNetfx35Installed()
{
bool found = false;
int regValue = 0;
if (GetRegistryValue(RegistryHive.LocalMachine, Netfx35RegKeyName, Netfx11PlusRegValueName, RegistryValueKind.DWord, out regValue))
{
if (regValue == 1)
{
found = true;
}
}
return found;
}
#endregion
#endregion
#endregion
#endregion
#region public properties and methods
#region methods
#region IsInstalled
#region IsInstalled(FrameworkVersion frameworkVersion)
/// <summary>
/// Determines if the specified .NET Framework version is installed
/// on the local computer.
/// </summary>
/// <param name="frameworkVersion">One of the
/// <see cref="FrameworkVersion"/> values.</param>
/// <returns><see langword="true"/> if the specified .NET Framework
/// version is installed; otherwise <see langword="false"/>.</returns>
public static bool IsInstalled(FrameworkVersion frameworkVersion)
{
bool ret = false;
switch (frameworkVersion)
{
case FrameworkVersion.Fx20:
ret = IsNetfx20Installed();
break;
case FrameworkVersion.Fx30:
ret = IsNetfx30Installed();
break;
case FrameworkVersion.Fx35:
ret = IsNetfx35Installed();
break;
case FrameworkVersion.Fx35Sp1:
ret = IsNetfx35SP1Installed();
break;
default:
break;
}
return ret;
}
#endregion
#endregion
#region GetExactVersion
#region GetExactVersion(FrameworkVersion frameworkVersion)
/// <summary>
/// Retrieves the exact version number for the specified .NET Framework
/// version.
/// </summary>
/// <param name="frameworkVersion">One of the
/// <see cref="FrameworkVersion"/> values.</param>
/// <returns>A <see cref="Version">version</see> representing
/// the exact version number for the specified .NET Framework version.
/// If the specified .NET Frameowrk version is not found, a
/// <see cref="Version"/> is returned that represents a 0.0.0.0 version
/// number.
/// </returns>
public static Version GetExactVersion(FrameworkVersion frameworkVersion)
{
Version fxVersion = new Version();
switch (frameworkVersion)
{
case FrameworkVersion.Fx20:
fxVersion = GetNetfx20ExactVersion();
break;
case FrameworkVersion.Fx30:
fxVersion = GetNetfx30ExactVersion();
break;
case FrameworkVersion.Fx35:
fxVersion = GetNetfx35ExactVersion();
break;
default:
break;
}
return fxVersion;
}
#endregion
#endregion
#endregion
#endregion
}
#endregion
public enum SampleVersion
{
v35
}
public class Utility
{
public static string GetSampleVersionFolderName(SampleVersion version)
{
string ver = string.Empty;
switch (version)
{
case SampleVersion.v35:
ver = "3.5";
break;
default:
ver = "3.5";
break;
}
return ver;
}
}
public class ReportViewerHelper
{
public static string GetReportPath(string path)
{
string _dataPath = GetCommonFolder(new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath));
_dataPath += @"\EjReportTemplate\" + path;
return _dataPath;
}
static string GetCommonFolder(DirectoryInfo dtInfo)
{
var _folderNames = dtInfo.GetDirectories("Common");
if (_folderNames.Length > 0)
{
var _innerfolder = _folderNames[0].GetDirectories("Data");
if (_innerfolder.Length > 0)
return _innerfolder[0].FullName;
}
return dtInfo.Parent != null ? GetCommonFolder(dtInfo.Parent) : string.Empty;
}
}
}