string path = "";
//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。 结果:E:\项目\Test
path = Environment.CurrentDirectory;
// 获取程序的基目录。结果:E:\项目\Test\
path = System.AppDomain.CurrentDomain.BaseDirectory;
// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。 结果:E:\项目\Test
path = System.Environment.CurrentDirectory ;
// 获取应用程序的当前工作目录,注意工作目录是可以改变的,而不限定在程序所在目录。 结果:E:\项目\Test
path = System.IO.Directory.GetCurrentDirectory();
// 获取和设置包括该应用程序的目录的名称。 结果:E:\项目\Test\
path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
//获取项目下的Log文件路径 结果:E:\项目\Test\Log
path = System.Web.HttpContext.Current.Server.MapPath("~/Log");
// 获取程序的基目录。
System.AppDomain.CurrentDomain.BaseDirectory
// 获取模块的完整路径。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
System.Environment.CurrentDirectory
// 获取应用程序的当前工作目录。
System.IO.Directory.GetCurrentDirectory()
// 获取和设置包括该应用程序的目录的名称。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
// 获取启动了应用程序的可执行文件的路径。
System.Windows.Forms.Application.StartupPath
// 获取启动了应用程序的可执行文件的路径及文件名
System.Windows.Forms.Application.ExecutablePath
C# WinForm中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别示例如下:
private void Frm_Server_Load(object sender, EventArgs e)
{
MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);
MessageBox.Show(Application.StartupPath );
}
说明:
1. AppDomain.CurrentDomain.BaseDirectory 返回结果为: D:\mycode\
Application.StartupPath 返回结果为: D:\mycode
2. Application.StartupPath 只能用于WinForm窗体中,而AppDomain.CurrentDomain.BaseDirectory既可以用于WinForm窗体中,也可以用于类库DLL文件中.
3. HttpContext.Current.Request.Url.Host.ToString() 返回结果为:http://locahost.XXXX.XXX
HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
//Silverlight获得站点根目录:
var _xapUri = Application.Current.Host.Source;
var _webUri = new Uri(_xapUri, "../");
在我的 asp.net 中它创建在 IIS 中,这是唯一到服务器的时候,就会给我想查找该名称的 web 应用程序。我不会感兴趣,但实际的名字在 IIS 中的站点的 web 站点的域名。
我必须要能够可靠地做这事的 IIS6 和 7。
要弄清楚我说给定的名称,在 IIS、 而不是域名和不是虚拟目录的路径。
解决方法 1:
System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
解决方法 2:
这里是一个 帖子相关 检索站点 id。
这里的一些代码,可能会为您的工作:
using System.DirectoryServices;
using System;
public class IISAdmin
{
public static void GetWebsiteID(string websiteName)
{
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
foreach(DirectoryEntry de in w3svc.Children)
{
if(de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
{
Console.Write(de.Name);
}
}
}
public static void Main()
{
GetWebsiteID("Default Web Site");
}
}
这里是 原始的帖子的链接。
我不确定它将致力于 IIS7,但如果您为它应该工作的 IIS7 安装 IIS6 兼容组件。
解决方法 3:
您正在寻找 ServerManager (Microsoft.Web.Administration) 提供的读取和写入访问,IIS 7.0 配置系统。
循环访问 Microsoft.Web.Administration.SiteCollection、 获取对您的网站使用站点对象的引用,并读取名称属性的值。
// Snippet
using (ServerManager serverManager = new ServerManager()) {
var sites = serverManager.Sites;
foreach (Site site in sites) {
Console.WriteLine(site.Name); // This will return the WebSite name
}
您还可以使用 LINQ 查询 ServerManager.Sites 集合 (请参见下面的示例)
// Start all stopped WebSites using the power of Linq :)
var sites = (from site in serverManager.Sites
where site.State == ObjectState.Stopped
orderby site.Name
select site);
foreach (Site site in sites) {
site.Start();
}
注 : Microsoft.Web.Administration 仅 IIS7的工作。
IIS6,您可以使用 ADSI 和 WMI 这样做,但我建议你去为 WMI ADSI 比快。如果使用 WMI,我们看看 WMI 代码创建者 1.0 (免费 / 发达的 Microsoft)。它将为您生成的代码。
禾田
解决方法 4:
这篇文章为您提供了了解如何,您可以检索 IIS 信息并创建网站/虚拟目录。
解决方法 5:
您需要先做 ServerManager.OpenRemote("serverName"),当连接到远程服务器。
它的基本做法这样的
using (ServerManager srvMgr = ServerManager.OpenRemote("serverName"))
{
}