예제 #1
0
        public int CreateDir(string siteName, string virtualPath, string physicalPath, bool enableAllMimeTypes)
        {
            int errorCode = ErrorCode.Succeed;

            try
            {
                var siteEntry = new DirectoryEntry("IIS://localhost/w3svc");

                //先找站点
                DirectoryEntry targetSite = null;
                foreach(DirectoryEntry site in siteEntry.Children)
                {
                    if(!site.SchemaClassName.EqualsEx("IIsWebServer"))
                        continue;

                    string name = Convert.ToString(site.Properties["ServerComment"].Value);
                    if(!name.IsNullOrEmpty() && name.EqualsEx(siteName))
                    {
                        targetSite = site;
                        break;
                    }
                }

                if(targetSite != null)
                {
                    //根目录
                    var rootEntry = new DirectoryEntry(string.Format("IIS://localhost/w3svc/{0}/ROOT", targetSite.Name));
                    bool exists = false;

                    foreach(DirectoryEntry dir in rootEntry.Children)
                    {
                        //IIsWebVirtualDir表示虚拟目录
                        if(!dir.SchemaClassName.EqualsEx("IIsWebVirtualDir"))
                            continue;

                        //名称不包含/,不会是多级
                        if(dir.Name.Equals(virtualPath.Substring(1)))
                        {
                            exists = true;
                            break;
                        }
                    }

                    if(!exists)
                    {
                        var dir = rootEntry.Children.Add(virtualPath.Substring(1), "IIsWebVirtualDir");
                        dir.Properties["Path"][0] = physicalPath;								//物理路径
                        dir.Properties["AppFriendlyName"][0] = virtualPath.Substring(1);		//名称
                        dir.Properties["AccessRead"][0] = true;									//读权限
                        dir.Properties["DontLog"][0] = true;

                        if(enableAllMimeTypes)
                        {
                            new IISOle.MimeMapClass();
                            var mime = new IISOle.MimeMapClass();
                            mime.Extension = "*";
                            mime.MimeType = "application/octet-stream";
                            dir.Properties["MimeMap"].Clear();
                            dir.Properties["MimeMap"].Add(mime);
                        }

                        dir.CommitChanges();
                        rootEntry.CommitChanges();
                    }
                    else
                    {
                        errorCode = ErrorCode.AppExists;
                    }
                }
                else
                {
                    errorCode = ErrorCode.SiteNotFound;
                }
            }
            catch(Exception ex)
            {
                errorCode = ErrorCode.Unknown;
                Console.WriteLine(ex.Message);
            }

            return errorCode;
        }
예제 #2
0
        public int CreateDir(string siteName, string virtualPath, string physicalPath, bool enableAllMimeTypes)
        {
            int errorCode = ErrorCode.Succeed;

            try
            {
                var siteEntry = new DirectoryEntry("IIS://localhost/w3svc");

                //先找站点
                DirectoryEntry targetSite = null;
                foreach (DirectoryEntry site in siteEntry.Children)
                {
                    if (!site.SchemaClassName.EqualsEx("IIsWebServer"))
                    {
                        continue;
                    }

                    string name = Convert.ToString(site.Properties["ServerComment"].Value);
                    if (!name.IsNullOrEmpty() && name.EqualsEx(siteName))
                    {
                        targetSite = site;
                        break;
                    }
                }

                if (targetSite != null)
                {
                    //根目录
                    var  rootEntry = new DirectoryEntry(string.Format("IIS://localhost/w3svc/{0}/ROOT", targetSite.Name));
                    bool exists    = false;

                    foreach (DirectoryEntry dir in rootEntry.Children)
                    {
                        //IIsWebVirtualDir表示虚拟目录
                        if (!dir.SchemaClassName.EqualsEx("IIsWebVirtualDir"))
                        {
                            continue;
                        }

                        //名称不包含/,不会是多级
                        if (dir.Name.Equals(virtualPath.Substring(1)))
                        {
                            exists = true;
                            break;
                        }
                    }

                    if (!exists)
                    {
                        var dir = rootEntry.Children.Add(virtualPath.Substring(1), "IIsWebVirtualDir");
                        dir.Properties["Path"][0]            = physicalPath;                                                    //物理路径
                        dir.Properties["AppFriendlyName"][0] = virtualPath.Substring(1);                                        //名称
                        dir.Properties["AccessRead"][0]      = true;                                                            //读权限
                        dir.Properties["DontLog"][0]         = true;

                        if (enableAllMimeTypes)
                        {
                            new IISOle.MimeMapClass();
                            var mime = new IISOle.MimeMapClass();
                            mime.Extension = "*";
                            mime.MimeType  = "application/octet-stream";
                            dir.Properties["MimeMap"].Clear();
                            dir.Properties["MimeMap"].Add(mime);
                        }

                        dir.CommitChanges();
                        rootEntry.CommitChanges();
                    }
                    else
                    {
                        errorCode = ErrorCode.AppExists;
                    }
                }
                else
                {
                    errorCode = ErrorCode.SiteNotFound;
                }
            }
            catch (Exception ex)
            {
                errorCode = ErrorCode.Unknown;
                Console.WriteLine(ex.Message);
            }

            return(errorCode);
        }