(1) 例子:
用IIS举个例子:
WEB服务目录是d:\路径
那么用HTTP访问网站根目录的时候,其实访问的是d:\路径,那么其中虚拟路径就是\(根),物理路径就是d:\路径
(2) 我自己的理解:绝对路径一般都是带有磁盘完成路径, 而虚拟路径一般不带有磁盘
2 程序中虚拟路径和物理路径的转化
#region 物理路径和相对路径的转换
//本地路径转换成URL相对路径
private string urlconvertor(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString())//获取程序根目录
string imagesurl2 = imagesurl1.Replace(tmpRootDir, "")//转换成相对路径
imagesurl2 = imagesurl2.Replace(@"\", @"/")
//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"")
return imagesurl2
}
//相对路径转换成服务器本地物理路径
private string urlconvertorlocal(string imagesurl1)
{
string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString())//获取程序根目录
string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @"\")//转换成绝对路径
return imagesurl2
}
#endregion
3 下载的方法
/// <summary>
/// 获取物理地址
/// </summary>
public static string MapPathFile(string FileName)
{
return HttpContext.Current.Server.MapPath(FileName)
}
/// <summary>
/// 普通下载
/// </summary>
/// <param name="FileName">文件虚拟路径</param>
public static bool DownLoadold(string FileName)
{
bool bools = false
string destFileName = MapPathFile(FileName)
if (File.Exists(destFileName))
{
FileInfo fi = new FileInfo(destFileName)
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.Buffer = false
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), System.Text.Encoding.UTF8))
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString())
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.WriteFile(destFileName)
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.End()
bools = true
}
return bools
}
public static void ResponseFile(string path, HttpContext context)
{
context = HttpContext.Current
System.IO.Stream iStream = null
byte[] buffer = new Byte[10000]
int length
long dataToRead
string filename = System.IO.Path.GetFileName(path)
try
{
iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
dataToRead = iStream.Length
context.Response.ContentType = "application/octet-stream"
context.Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8))
while (dataToRead >0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000)
context.Response.OutputStream.Write(buffer, 0, length)
context.Response.Flush()
buffer = new Byte[10000]
dataToRead = dataToRead - length
}
else
{
dataToRead = -1
}
}
}
catch (Exception ex)
{
context.Response.Write(ex.Message)
}
finally
{
if (iStream != null)
{
iStream.Close()
}
}
}
public static void ResponseFile(string path, string fileName, HttpContext context)
{
context = HttpContext.Current
System.IO.Stream iStream = null
byte[] buffer = new Byte[10000]
int length
long dataToRead
try
{
iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
dataToRead = iStream.Length
context.Response.ContentType = "application/octet-stream"
context.Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
while (dataToRead >0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 10000)
context.Response.OutputStream.Write(buffer, 0, length)
context.Response.Flush()
buffer = new Byte[10000]
dataToRead = dataToRead - length
}
else
{
dataToRead = -1
}
}
}
catch (Exception ex)
{
context.Response.Write(ex.Message)
}
finally
{
if (iStream != null)
{
iStream.Close()
}
}
}
一、就是将你的配置文件安装在system32目录下二、在你的服务中通过注册表来获得这个服务的安装路径
/*
Windows服务在系统安装后会在注册表的 "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[ServiceName]"下以服务的ServiceName建1个目录,目录中会有"ImagePath"节,这里保存的就是该服务的安装路径。
*/
/// <summary>
/// 获取服务安装路径
/// </summary>
/// <param name="ServiceName"></param>
/// <returns></returns>
public static string GetWindowsServiceInstallPath(string ServiceName)
{
string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName
string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString()
//替换掉双引号
path = path.Replace("\"", string.Empty)
FileInfo fi = new FileInfo(path)
return fi.Directory.ToString()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)