public static VirtualWebDir FindLocalWebSite(string virtualDir)
        {
            DirectoryEntry site      = new DirectoryEntry(LocalWebPath);
            string         className = site.SchemaClassName.ToString();

            if ((className.EndsWith("Server", StringComparison.OrdinalIgnoreCase)) || (className.EndsWith("VirtualDir", StringComparison.OrdinalIgnoreCase)))
            {
                DirectoryEntries vdirs = site.Children;
                IEnumerator      ie    = vdirs.GetEnumerator();
                while (ie.MoveNext())
                {
                    DirectoryEntry de = ie.Current as DirectoryEntry;
                    if (de != null)
                    {
                        string sn = Path.GetFileName(de.Path);
                        if (string.Compare(virtualDir, sn, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            string dir = null;
                            PropertyValueCollection pv = de.Properties["Path"];
                            if (pv != null)
                            {
                                if (pv.Value != null)
                                {
                                    dir = pv.Value.ToString();
                                    VirtualWebDir vd = new VirtualWebDir(de.Path, dir);
                                    return(vd);
                                }
                            }
                        }
                    }
                }
            }
            return(null);
        }
        public static IList <VirtualWebDir> GetVirtualDirectories(string metabasePath)
        {
            List <VirtualWebDir> list = new List <VirtualWebDir>();
            DirectoryEntry       site = new DirectoryEntry(metabasePath);
            string className          = site.SchemaClassName.ToString();

            if ((className.EndsWith("Server", StringComparison.OrdinalIgnoreCase)) || (className.EndsWith("VirtualDir", StringComparison.OrdinalIgnoreCase)))
            {
                DirectoryEntries vdirs = site.Children;
                IEnumerator      ie    = vdirs.GetEnumerator();
                while (ie.MoveNext())
                {
                    DirectoryEntry de = ie.Current as DirectoryEntry;
                    if (de != null)
                    {
                        string dir = null;
                        PropertyValueCollection pv = de.Properties["Path"];
                        if (pv != null)
                        {
                            if (pv.Value != null)
                            {
                                dir = pv.Value.ToString();
                                VirtualWebDir vd = new VirtualWebDir(de.Path, dir);
                                list.Add(vd);
                            }
                        }
                    }
                }
            }
            return(list);
        }
        private void buttonFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();

            try
            {
                dlg.Description         = "Select a physical folder for the new web site";
                dlg.ShowNewFolderButton = true;
                dlg.SelectedPath        = textBoxPdir.Text;
            }
            catch
            {
            }
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                if (VirtualWebDir.IsNetworkDrive(dlg.SelectedPath))
                {
                    MessageBox.Show(this, "A network folder cannot be used for creating a web site", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    textBoxPdir.Text = dlg.SelectedPath;
                }
            }
        }
Пример #4
0
 private void buttonOK_Click(object sender, EventArgs e)
 {
     if (listView1.SelectedItems != null && listView1.SelectedItems.Count > 0)
     {
         Ret = listView1.SelectedItems[0].Tag as VirtualWebDir;
         if (Ret != null)
         {
             this.DialogResult = DialogResult.OK;
         }
     }
 }
Пример #5
0
        private void buttonNew_Click(object sender, EventArgs e)
        {
            DialogNewWeb dlg = new DialogNewWeb();

            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                Ret = dlg.Ret;
                ListViewItem vi = new ListViewItem(new string[] { Ret.WebName, Ret.PhysicalDirectory });
                vi.Tag = Ret;
                listView1.Items.Add(vi);
                vi.Selected = true;
            }
        }
 public static VirtualWebDir FindLocalWebSiteByName(Form owner, string webName, out bool iisError)
 {
     iisError = false;
     using (ShowMessage frm = new ShowMessage(owner, "Checking web site name..."))
     {
         DirectoryEntry site      = new DirectoryEntry(LocalWebPath);
         string         className = string.Empty;
         try
         {
             className = site.SchemaClassName.ToString();
         }
         catch (Exception err)
         {
             StringBuilder sb = new StringBuilder();
             while (err != null)
             {
                 sb.Append("Error finding web site via Active Directory. Please install 'IIS6 Metabase compatibility' if it is not installed.\r\nError message:");
                 sb.Append(err.Message);
                 sb.Append("\r\nStack trace:");
                 if (!string.IsNullOrEmpty(err.StackTrace))
                 {
                     sb.Append(err.StackTrace);
                 }
                 sb.Append("===============\r\n");
                 err = err.InnerException;
             }
             MessageBox.Show(sb.ToString(), "Find web site", MessageBoxButtons.OK, MessageBoxIcon.Error);
             iisError = true;
             return(null);
         }
         if ((className.EndsWith("Server", StringComparison.OrdinalIgnoreCase)) || (className.EndsWith("VirtualDir", StringComparison.OrdinalIgnoreCase)))
         {
             DirectoryEntries vdirs = site.Children;
             IEnumerator      ie    = vdirs.GetEnumerator();
             while (ie.MoveNext())
             {
                 DirectoryEntry de = ie.Current as DirectoryEntry;
                 if (de != null)
                 {
                     if (string.Compare(de.Name, webName, StringComparison.OrdinalIgnoreCase) == 0)
                     {
                         string dir = null;
                         PropertyValueCollection pv = de.Properties["Path"];
                         if (pv != null)
                         {
                             if (pv.Value != null)
                             {
                                 dir = pv.Value.ToString();
                                 VirtualWebDir vd = new VirtualWebDir(de.Path, dir);
                                 return(vd);
                             }
                         }
                         else
                         {
                         }
                     }
                 }
             }
         }
     }
     return(null);
 }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool bOK = false;

            buttonOK.Enabled     = false;
            buttonCancel.Enabled = false;
            this.Cursor          = Cursors.WaitCursor;
            string svDir = textBoxVdir.Text.Trim();

            if (svDir.Length == 0)
            {
                MessageBox.Show(this, "Web site name is empty", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                if (svDir.Contains(' ') || svDir.Contains('\t'))
                {
                    MessageBox.Show(this, "Do not use spaces and tabs in the web site name", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    Regex rg = new Regex(@"^([\w])*$");
                    if (rg.IsMatch(svDir))
                    {
                        string spDir = textBoxPdir.Text.Trim();
                        if (spDir.Length == 0)
                        {
                            MessageBox.Show(this, "Web site folder is empty", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                        else
                        {
                            if (VirtualWebDir.IsNetworkDrive(spDir))
                            {
                                MessageBox.Show(this, "Cannot use a network folder for web site", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                            else
                            {
                                bOK = Directory.Exists(spDir);
                                if (!bOK)
                                {
                                    try
                                    {
                                        Directory.CreateDirectory(spDir);
                                        bOK = true;
                                    }
                                    catch (Exception er)
                                    {
                                        bOK = false;
                                        MessageBox.Show(this, er.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                    }
                                }
                            }
                            if (bOK && Directory.Exists(spDir))
                            {
                                bool exists = false;
                                try
                                {
                                    IList <VirtualWebDir> vs = IisUtility.GetVirtualDirectories(IisUtility.LocalWebPath);
                                    foreach (VirtualWebDir v in vs)
                                    {
                                        if (string.Compare(v.WebName, svDir, StringComparison.OrdinalIgnoreCase) == 0)
                                        {
                                            exists = true;
                                            if (string.Compare(v.PhysicalDirectory, spDir, StringComparison.OrdinalIgnoreCase) == 0)
                                            {
                                                MessageBox.Show(this, "The web site is already created", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                Ret = v;
                                            }
                                            else
                                            {
                                                MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Web site name is used by folder {0}", v.PhysicalDirectory), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                                bOK = false;
                                            }
                                            break;
                                        }
                                        if (string.Compare(v.PhysicalDirectory, spDir, StringComparison.OrdinalIgnoreCase) == 0)
                                        {
                                            MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Web site folder is used by web site {0}.", v.WebName), Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                            Ret    = v;
                                            exists = true;
                                            bOK    = false;
                                            break;
                                        }
                                    }
                                    if (!exists)
                                    {
                                        try
                                        {
                                            IisUtility.CreateLocalWebSite(svDir, spDir);
                                            Ret = new VirtualWebDir(svDir, spDir);
                                        }
                                        catch (Exception err)
                                        {
                                            bOK = false;
                                            MessageBox.Show(this, err.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                        }
                                    }
                                }
                                catch (Exception err)
                                {
                                    bOK = false;
                                    MessageBox.Show(this, err.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                                if (bOK)
                                {
                                    //set access permissions
                                    VirtualWebDir.AddDirectorySecurity(spDir);
                                }
                            }
                            else
                            {
                                MessageBox.Show(this, "Web site folder does not exist", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "Please use only alphanumeric characters in Web site name", Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            if (bOK)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                buttonOK.Enabled     = true;
                buttonCancel.Enabled = true;
                this.Cursor          = Cursors.Default;
            }
        }