1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.EnterpriseServices.Internal;
7
using System.Reflection;
9
using ICSharpCode.Core;
10
using Microsoft.Win32;
12
namespace ICSharpCode.SharpDevelop.Project
14
public enum IISVersion
32
/// Exposes common operations used in Web Projects
34
public static class WebProjectService
36
const string IIS_LOCATION = "Software\\Microsoft\\InetStp";
37
const string IIS_MAJOR_VERSION = "MajorVersion";
38
const string IIS_INSTALL_PATH = "InstallPath";
39
const string DEFAULT_WEB_SITE = "Default Web Site";
40
const string IIS_WEB_LOCATION = "IIS://localhost/W3SVC/1/Root";
42
const string ASPNET_REG_PATH = @"SOFTWARE\MICROSOFT\ASP.NET";
43
const string ASPNET_ROOT_VER = @"RootVer";
45
const string FRAMEWORK_LOCATION = @"%systemroot%\Microsoft.NET\";
46
const string FRAMEWORK32 = @"Framework\";
47
const string FRAMEWORK64 = @"Framework64\";
50
/// Gets "iisexpress" string.
52
public const string IIS_EXPRESS_PROCESS_NAME = "iisexpress";
55
/// Gets "aspnet_wp" string.
57
public const string IIS_5_PROCESS_NAME = "aspnet_wp";
60
/// Gets "w3wp" string.
62
public const string IIS_NEW_PROCESS_NAME = "w3wp";
65
/// Gets IIS Express process location.
67
public static string IISExpressProcessLocation {
69
return Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) +
70
@"\IIS Express\iisexpress.exe";
75
/// Gets the IIS worker process name.
77
public static string WorkerProcessName {
80
return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
88
name = IIS_5_PROCESS_NAME;
90
case IISVersion.IISExpress:
91
name = IIS_EXPRESS_PROCESS_NAME;
94
name = IIS_NEW_PROCESS_NAME;
100
catch (Exception ex) {
106
public static string WorkerProcessLocation {
109
return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
116
case IISVersion.IIS5:
117
location = FRAMEWORK_LOCATION + (Environment.Is64BitOperatingSystem ? FRAMEWORK64 : FRAMEWORK32);
119
string frameworkString = "";
121
RegistryService.GetRegistryValue<string>(
122
RegistryHive.LocalMachine,
125
RegistryValueKind.String,
126
out frameworkString);
127
int ind = frameworkString.LastIndexOf('.');
128
location += "v" + frameworkString.Substring(0, ind) + "\\";
133
string regValue = "";
135
RegistryService.GetRegistryValue<string>(
136
RegistryHive.LocalMachine,
139
RegistryValueKind.String,
141
location = regValue + "\\";
147
catch (Exception ex) {
154
/// Gets a value representing whether IIS is installed.
156
public static bool IsIISInstalled {
158
return (int)IISVersion > 4;
163
/// Gets a value representing IIS version.
165
public static IISVersion IISVersion
170
RegistryService.GetRegistryValue<int>(
171
RegistryHive.LocalMachine,
174
RegistryValueKind.DWord,
178
return (IISVersion)regValue;
180
if (File.Exists(IISExpressProcessLocation))
181
return IISVersion.IISExpress;
183
return IISVersion.None;
188
/// Creates a virtual directory in local IIS or IIS Express.
190
/// <param name="virtualDirectoryName">Virtual directory name.</param>
191
/// <param name="virtualDirectoryPath">Physical path.</param>
192
/// <returns></returns>
193
public static string CreateVirtualDirectory(string virtualDirectoryName, string physicalDirectoryPath)
197
return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
203
case IISVersion.IIS5:
204
case IISVersion.IIS6:
205
var vr = new IISVirtualRoot();
206
vr.Create(IIS_WEB_LOCATION,
207
physicalDirectoryPath,
208
virtualDirectoryName,
213
// TODO: find a better way to create IIS 7 applications without Microsoft.Web.Administration.ServerManager
214
string name = "/" + virtualDirectoryName;
215
// load from GAC - IIS7 is installed
216
Assembly webAdministrationAssembly;
219
webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
223
webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
226
// use dynamic because classic reflection is way TOO ugly
227
dynamic manager = webAdministrationAssembly.CreateInstance("Microsoft.Web.Administration.ServerManager");
229
if (manager.Sites[DEFAULT_WEB_SITE] != null) {
230
if (manager.Sites[DEFAULT_WEB_SITE].Applications[name] == null) {
231
manager.Sites[DEFAULT_WEB_SITE].Applications.Add(name, physicalDirectoryPath);
232
manager.CommitChanges();
233
error = string.Empty;
235
error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
238
if (manager.Sites[0].Applications[name] == null) {
239
manager.Sites[0].Applications.Add(name, physicalDirectoryPath);
240
manager.CommitChanges();
241
error = string.Empty;
243
error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
252
catch (Exception ex) {