IIS - Create Application and Set Application Pool (C#) (edit)
Chú ý:
+ Mở Visual Studio 2017 với quyền Administrator
+ Cài đặt Web Management Tools
Input
+ Tên Root Application: TSR
+ Tên Application: LightServices
+ Tên Customer: 412687
Output
+ Tạo Application nếu chưa tồn tại.
+ Sử dụng XCopy để copy toàn bộ files từ một nguồn cho trước đến Physical Path của Application vừa tạo.
+ Hoặc Unzip từ một nguồn là 1 file .zip cho trước.
+ Set ApplicationPool trùng với ApplicationPool của Root Application.
Solution
public class IISHelper { private static string AppLightServices = "LightServices"; private static string AppServices = "Services"; private static string AppWebadmin = "Webadmin"; private static string ApplicationPoolName = string.Empty; // TSR private static string AppPath = string.Empty; // D:\TSRLight\412687\LightServices\ private static string VirtualRootPhysicalPath = string.Empty; // D:\TSRLight\ private static bool IsLightServicesNotExists = true; // False // Return 0: Not run
// Return 1: Run success
// Return -1: Throw exception public static int CreateApplicationLightServices(string customerCode, string siteName = "TSR", string sourceAppPath = "") { try { using (ServerManager manager = new ServerManager()) { foreach (Site s in manager.Sites) { foreach (Application app in s.Applications) { if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppServices)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppWebadmin)) { ApplicationPoolName = app.ApplicationPoolName; var applicationRoot = s.Applications.Where(a => a.Path == "/").Single(); var virtualRoot = applicationRoot.VirtualDirectories.Where(v => v.Path == "/").Single(); VirtualRootPhysicalPath = virtualRoot.PhysicalPath; } if (s.Name.ToLower().Equals(siteName.ToLower()) && app.Path.Contains(customerCode) && app.Path.Contains(AppLightServices)) { IsLightServicesNotExists = false; } } } if (IsLightServicesNotExists && !string.IsNullOrWhiteSpace(ApplicationPoolName) && !string.IsNullOrWhiteSpace(VirtualRootPhysicalPath) && Directory.Exists(VirtualRootPhysicalPath)) { AppPath = Path.Combine(VirtualRootPhysicalPath, customerCode, AppLightServices); if (!string.IsNullOrWhiteSpace(AppPath) && !Directory.Exists(AppPath)) { Directory.CreateDirectory(AppPath); if (!string.IsNullOrWhiteSpace(sourceAppPath) && Directory.Exists(sourceAppPath)) { ProcessXcopy(sourceAppPath, AppPath); } else { var filePath = Path.Combine(Directory.GetCurrentDirectory(), "LightServices.zip"); if (File.Exists(filePath)) { ZipFile.ExtractToDirectory(filePath, AppPath); } } Site defaultSite = manager.Sites[siteName]; var app = defaultSite.Applications.Add($"/{customerCode}/{AppLightServices}", AppPath); app.ApplicationPoolName = ApplicationPoolName; manager.CommitChanges(); return 1; } else { return 0; } } else { return 0; } } } catch { return -1; } } /// /// /// private static void ProcessXcopy(string sourceDirectory, string targetDirectory) { // Use ProcessStartInfo class ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.CreateNoWindow = false; startInfo.UseShellExecute = false; //Give the name as Xcopy startInfo.FileName = "xcopy"; //make the window Hidden startInfo.WindowStyle = ProcessWindowStyle.Hidden; //Send the Source and destination as Arguments to the process startInfo.Arguments = "\"" + sourceDirectory + "\"" + " " + "\"" + targetDirectory + "\"" + @" /e /y /I"; try { // Start the process with the info we specified. // Call WaitForExit and then the using statement will close. using (Process exeProcess = Process.Start(startInfo)) { exeProcess.WaitForExit(); } } catch (Exception exp) { throw exp; } } }
Tham khảo
Sử dụng thư viện Microsoft.Web.Administration.dll
Sử dụng thư viện System.IO.Compression.dll và System.IO.Compression.FileSystem.dll
1) https://forums.iis.net/t/1207235.aspx?How+to+convert+a+physical+folder+into+an+Asp+NET+Application+using+Microsoft+Web+Administration+C+API
2) https://stackoverflow.com/questions/3228813/how-to-get-websites-physical-path-on-local-iis-server-from-a-desktop-app
3) http://www.anotherchris.net/csharp/6-ways-to-get-the-current-directory-in-csharp/
4) https://dailydotnettips.com/different-ways-of-getting-path/
5) https://www.c-sharpcorner.com/UploadFile/jawedmd/xcopy-using-C-Sharp-to-copy-filesfolders/
6) https://stackoverflow.com/questions/32236796/fastest-way-to-copy-files-from-one-directory-to-another (có sử dụng dữ liệu trong Database và Sql Server Connection)
https://www.c-sharpcorner.com/UploadFile/satisharveti/programmatically-create-iis-website-and-application-pool-usi/
https://stackoverflow.com/questions/16417578/iis-application-creation-by-using-microsoft-web-administration-library-creates-t
https://www.c-sharpcorner.com/UploadFile/225740/introduction-to-application-pool-in-iis/
https://stackoverflow.com/questions/12147721/change-apppool-of-application-programatically