public static bool ChangeApplicationFolder(string serverName, string path, string newSiteFolder) { try { string[] tokens = path.Split('\\'); if (tokens.Length < 4) { return false; } string siteName = tokens[tokens.Length - 1]; lock (_lock) { ServerManager server; Site site; GetSiteAndServer(serverName, siteName, out server, out site); if (site == null) { return false; } tokens[tokens.Length - 1] = newSiteFolder; string maintenancePath = GetPhysicalPath(tokens); site.Applications["/"].VirtualDirectories["/"].PhysicalPath = maintenancePath; server.CommitChanges(); } return true; } catch (Exception ex) { return false; } } private static void GetSiteAndServer(string serverName, string siteName, out ServerManager server, out Site site) { server = (string.IsNullOrEmpty(serverName) || serverName == "127.0.0.1") ? new ServerManager() : ServerManager.OpenRemote(serverName); site = null; if (server != null) { site = server.Sites.FirstOrDefault(s => s.Name.ToLower() == siteName.ToLower()); } } private static string GetPhysicalPath(string[] tokens) { ListnewTokens = new List (); for (int i = 3; i < tokens.Length; i++) { newTokens.Add(tokens[i]); } string originalPath = newTokens.Aggregate((a, b) => a + "\\" + b); originalPath = originalPath.Replace('$', ':'); return originalPath; }
Radi's Tech Blog
Thursday, December 27, 2012
Switch web application over IIS to redirect to other application in the same server
Following code switches web application over IIS to redirect to other application in the same server.
Inputs of the code: "serverName" - server name of the application which hosts applications over IIS, "path" - physical path of the application folder, "newSiteFolder" - folder name of the application we want redirect to
Retrieve historic changesets from tfs
Following code retrieves changesets between given dates from tfs:
public static ListGetChangesetsBetweenDates(string sourceControlPath, DateTime fromDate, DateTime toDate) { TfsTeamProjectCollection projectCollection = TfsManager.Instance.ProjectCollection; VersionControlServer versionControl = projectCollection.GetService (); VersionSpec versionFrom = GetDateVSpec(fromDate); VersionSpec versionTo = GetDateVSpec(toDate); IEnumerable results = versionControl.QueryHistory( sourceControlPath, VersionSpec.Latest, 0, RecursionType.Full, null, versionFrom, versionTo, int.MaxValue, true, true); List changesets = results.Cast ().ToList(); return changesets; } private static VersionSpec GetDateVSpec(DateTime date) { string dateSpec = string.Format("D{0:yyy}-{0:MM}-{0:dd}T{0:HH}:{0:mm}", date); return VersionSpec.ParseSingleSpec(dateSpec, ""); }
Get referrer's controller name in MVC application
Following code retrieves referrer's controller name in .net MVC application:
private string GetReferrerControlerName() { var fullUrl = this.Request.UrlReferrer.ToString(); string url = fullUrl; var request = new HttpRequest(null, url, null); var response = new HttpResponse(new StringWriter()); var httpContext = new HttpContext(request, response); var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext)); var values = routeData.Values; string controllerName = values["controller"].ToString(); return controllerName; }
Wednesday, December 26, 2012
Load all referenced assemblies in C#
Following code loads all referenced assemblies of the application. This code goes to "bin" folder and finds all dlls. We could use reflection to do the same, but the problem is when you need assemblies on start up, because of the JIT(Just in time) mechanism of .NET not all the assemblies is yet loaded and thus reflection will not return all referenced assemblies.
private ArrayList GetRefferencedAssemblies() { AppDomain domain = AppDomain.CurrentDomain; var assemblies = new ArrayList(); string[] files = Directory.GetFiles(domain.RelativeSearchPath, "*.dll", SearchOption.TopDirectoryOnly); foreach (var file in files) { AssemblyName assemblyName = AssemblyName.GetAssemblyName(file); assemblies.Add(assemblyName); } return assemblies; }
Subscribe to:
Posts (Atom)