当前位置: 首页 > 编程语言 > C#

C#获取应用程序路径或网页目录路径分享

时间:2023-04-10 13:26:05 C#

一、Winform获取本程序路径1、获取当前目录,返回最后一个不带“”的目录:如D:WinformbinDebugSystem.Windows。形式。应用程序启动路径;系统.环境.当前目录;System.IO.Directory.GetCurrentDirectory();返回最后一个带“”的目录(AppDomain应用域):如D:WinformbinDebugSystem.AppDomain.CurrentDomain.BaseDirectory;系统.AppDomain。CurrentDomain.SetupInformation.ApplicationBase;2.获取当前文件路径System.Windows.Forms.Application.ExecutablePath;System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;System.Reflection.Assembly.GetExecutingAssembly().CodeBase;//或者System.Reflection.Assembly.GetAssembly(typeof(classname)).CodeBase;//使用反射获取当前程序集的位置typeof(classname).Assembly.Location;/使用反射2.WebForm获取文件路径虚拟目录名称:WebSite1指向:E:mistools这个网页:http://localhost/WebSite1/folder/WebForm1.aspx1.获取虚拟目录root的相对路径:System.Web.HttpRuntime.AppDomainAppVirtualPath;请求.ApplicationPath;根绝对路径:AppDomain.CurrentDomain.BaseDirectory;请求.PhysicalApplicaitonP啊;Server.MapPath(“~”)\Server.MapPath("/WebSite1")2.获取文件路径当前文件相对路径,绝对路径Request.Path//相对路径/WebSite1/folder/WebForm1.aspxRequest.PhsicalPath//绝对路径E:mistoolsfolderWebForm1.aspxRequest.AppRelativeCurrentExecutionFilePath//~/folder/WebForm1.aspx当前目录Server.MapPath(".")orServer.MapPath("");//E:mistoolsfolderServer.MapPath("./1.jpg")orServer.MapPath("1.jpg");//E:mistoolsfolder1.jpg上一个目录Server.MapPath("..")//E:mistoolsServer.MapPath("../1.jpg")//(””);//E:mistools1.jpg1.JPG文件在上级目录Server.MapPath("../..")//C:inputpubwwwroot在上级目录上级目录,到顶层目录wwwroot根目录Server.MapPath("/")//C:inputpubwwwroot注意:在HTML文件中,使用“./”、“../”、“/”来表示相对路径和绝对路径C#获取文件路径的文章到此结束,希望对大家有所帮助给大家学习,希望大家多多支持,以上是C#学习教程:C#获取应用程序路径或网页目录路径共享的所有内容,如果对大家有用,还需要了解更多关于C#学习教程,希望大家多多关注,本文采集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如需转载请注明出处: