~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Main/Base/Project/Src/Services/WebProjectService/WebProjectService.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.EnterpriseServices.Internal;
 
6
using System.IO;
 
7
using System.Reflection;
 
8
 
 
9
using ICSharpCode.Core;
 
10
using Microsoft.Win32;
 
11
 
 
12
namespace ICSharpCode.SharpDevelop.Project
 
13
{
 
14
        public enum IISVersion
 
15
        {
 
16
                None = 0,
 
17
                IIS5 = 5,
 
18
                IIS6,
 
19
                IIS7,
 
20
                IISExpress,
 
21
                IIS_Future
 
22
        }
 
23
        
 
24
        public enum WebServer
 
25
        {
 
26
                None,
 
27
                IISExpress,
 
28
                IIS
 
29
        }
 
30
        
 
31
        /// <summary>
 
32
        /// Exposes common operations used in Web Projects
 
33
        /// </summary>
 
34
        public static class WebProjectService
 
35
        {
 
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";
 
41
                
 
42
                const string ASPNET_REG_PATH = @"SOFTWARE\MICROSOFT\ASP.NET";
 
43
                const string ASPNET_ROOT_VER = @"RootVer";
 
44
                
 
45
                const string FRAMEWORK_LOCATION = @"%systemroot%\Microsoft.NET\";
 
46
                const string FRAMEWORK32 = @"Framework\";
 
47
                const string FRAMEWORK64 = @"Framework64\";
 
48
                
 
49
                /// <summary>
 
50
                /// Gets &quot;iisexpress&quot; string.
 
51
                /// </summary>
 
52
                public const string IIS_EXPRESS_PROCESS_NAME = "iisexpress"; 
 
53
                
 
54
                /// <summary>
 
55
                /// Gets &quot;aspnet_wp&quot; string.
 
56
                /// </summary>
 
57
                public const string IIS_5_PROCESS_NAME = "aspnet_wp";
 
58
                
 
59
                /// <summary>
 
60
                /// Gets &quot;w3wp&quot; string.
 
61
                /// </summary>
 
62
                public const string IIS_NEW_PROCESS_NAME = "w3wp";
 
63
                
 
64
                /// <summary>
 
65
                /// Gets IIS Express process location.
 
66
                /// </summary>
 
67
                public static string IISExpressProcessLocation {
 
68
                        get {
 
69
                                return Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) +
 
70
                                        @"\IIS Express\iisexpress.exe";
 
71
                        }
 
72
                }
 
73
                
 
74
                /// <summary>
 
75
                /// Gets the IIS worker process name.
 
76
                /// </summary>
 
77
                public static string WorkerProcessName {
 
78
                        get {
 
79
                                if (!IsIISInstalled)
 
80
                                        return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
 
81
                                
 
82
                                try {
 
83
                                        string name;
 
84
                                        
 
85
                                        switch(IISVersion)
 
86
                                        {
 
87
                                                case IISVersion.IIS5:
 
88
                                                        name = IIS_5_PROCESS_NAME;
 
89
                                                        break;
 
90
                                                case IISVersion.IISExpress:
 
91
                                                        name = IIS_EXPRESS_PROCESS_NAME;
 
92
                                                        break;
 
93
                                                default:
 
94
                                                        name = IIS_NEW_PROCESS_NAME;
 
95
                                                        break;
 
96
                                        }
 
97
                                        
 
98
                                        return name;
 
99
                                }
 
100
                                catch (Exception ex) {
 
101
                                        return ex.Message;
 
102
                                }
 
103
                        }
 
104
                }
 
105
                
 
106
                public static string WorkerProcessLocation {
 
107
                        get {
 
108
                                if (!IsIISInstalled)
 
109
                                        return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
 
110
                                
 
111
                                try {
 
112
                                        string location;
 
113
                                        
 
114
                                        switch(IISVersion)
 
115
                                        {
 
116
                                                case IISVersion.IIS5:
 
117
                                                        location = FRAMEWORK_LOCATION + (Environment.Is64BitOperatingSystem ? FRAMEWORK64 : FRAMEWORK32);
 
118
                                                        
 
119
                                                        string frameworkString = "";
 
120
                                                        
 
121
                                                        RegistryService.GetRegistryValue<string>(
 
122
                                                                RegistryHive.LocalMachine,
 
123
                                                                ASPNET_REG_PATH,
 
124
                                                                ASPNET_ROOT_VER,
 
125
                                                                RegistryValueKind.String,
 
126
                                                                out frameworkString);
 
127
                                                        int ind = frameworkString.LastIndexOf('.');
 
128
                                                        location += "v" + frameworkString.Substring(0, ind) + "\\";
 
129
                                                        
 
130
                                                        break;
 
131
                                                        
 
132
                                                default:
 
133
                                                        string regValue = "";
 
134
                                                        
 
135
                                                        RegistryService.GetRegistryValue<string>(
 
136
                                                                RegistryHive.LocalMachine,
 
137
                                                                IIS_LOCATION,
 
138
                                                                IIS_INSTALL_PATH,
 
139
                                                                RegistryValueKind.String,
 
140
                                                                out regValue);
 
141
                                                        location = regValue + "\\";
 
142
                                                        break;
 
143
                                        }
 
144
                                        
 
145
                                        return location;
 
146
                                }
 
147
                                catch (Exception ex) {
 
148
                                        return ex.Message;
 
149
                                }
 
150
                        }
 
151
                }
 
152
                
 
153
                /// <summary>
 
154
                /// Gets a value representing whether IIS is installed.
 
155
                /// </summary>
 
156
                public static bool IsIISInstalled {
 
157
                        get {
 
158
                                return (int)IISVersion > 4;
 
159
                        }
 
160
                }
 
161
                
 
162
                /// <summary>
 
163
                /// Gets a value representing IIS version.
 
164
                /// </summary>
 
165
                public static IISVersion IISVersion
 
166
                {
 
167
                        get {
 
168
                                int regValue = 0;
 
169
                                
 
170
                                RegistryService.GetRegistryValue<int>(
 
171
                                        RegistryHive.LocalMachine,
 
172
                                        IIS_LOCATION,
 
173
                                        IIS_MAJOR_VERSION,
 
174
                                        RegistryValueKind.DWord,
 
175
                                        out regValue);
 
176
                                
 
177
                                if (regValue > 4)
 
178
                                        return (IISVersion)regValue;
 
179
                                
 
180
                                if (File.Exists(IISExpressProcessLocation))
 
181
                                        return IISVersion.IISExpress;
 
182
                                
 
183
                                return IISVersion.None;
 
184
                        }
 
185
                }
 
186
                
 
187
                /// <summary>
 
188
                /// Creates a virtual directory in local IIS or IIS Express.
 
189
                /// </summary>
 
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)
 
194
                {
 
195
                        try {
 
196
                                if (!IsIISInstalled)
 
197
                                        return ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.IISNotFound");
 
198
                                
 
199
                                string error;
 
200
                                
 
201
                                switch(IISVersion)
 
202
                                {
 
203
                                        case IISVersion.IIS5:
 
204
                                        case IISVersion.IIS6:
 
205
                                                var vr = new IISVirtualRoot();
 
206
                                                vr.Create(IIS_WEB_LOCATION,
 
207
                                                          physicalDirectoryPath,
 
208
                                                          virtualDirectoryName,
 
209
                                                          out error);
 
210
                                                break;
 
211
                                                
 
212
                                        default:
 
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;
 
217
                                                try {
 
218
                                                        // iis 7
 
219
                                                        webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
 
220
                                                }
 
221
                                                catch {
 
222
                                                        // iis express
 
223
                                                        webAdministrationAssembly = Assembly.Load("Microsoft.Web.Administration, Version=7.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
 
224
                                                }
 
225
                                                
 
226
                                                // use dynamic because classic reflection is way TOO ugly
 
227
                                                dynamic manager = webAdministrationAssembly.CreateInstance("Microsoft.Web.Administration.ServerManager");
 
228
                                                
 
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;
 
234
                                                        } else {
 
235
                                                                error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
 
236
                                                        }
 
237
                                                } else {
 
238
                                                        if (manager.Sites[0].Applications[name] == null) {
 
239
                                                                manager.Sites[0].Applications.Add(name, physicalDirectoryPath);
 
240
                                                                manager.CommitChanges();
 
241
                                                                error = string.Empty;
 
242
                                                        } else {
 
243
                                                                error = ResourceService.GetString("ICSharpCode.WepProjectOptionsPanel.ApplicationExists");
 
244
                                                        }
 
245
                                                }
 
246
                                                manager.Dispose();
 
247
                                                break;
 
248
                                }
 
249
                                
 
250
                                return error;
 
251
                        }
 
252
                        catch (Exception ex) {
 
253
                                return ex.Message;
 
254
                        }
 
255
                }
 
256
        }
 
257
}