您的位置:首页 > 编程语言 > C#

C# 获取当前目录的各种方法

2012-04-13 17:13 411 查看
Application.StartupPath;Environment.SpecialFolder.ApplicationData

Environment.SpecialFolder命名空间可以获取很多特定的路径目录。

Environment.SpecialFolder.

ApplicationData

CommonApplicationData

CommonProgramFiles

Cookies

DesktopDirectory

Favorites

History

InternetCache

LocalApplicationData

Personal

ProgramFiles

Programs

Recent

SendTo

StartMenu

Startup

Templates

System

System.Web.HttpContext.Request.{获取服务器绝对路径和虚拟目录路径}

System.AppDomain.CurrentDomain.BaseDirectory;获取应用程序的当前工作目录。

string path = System.IO.Directory.GetCurrentDirectory();Environment.CurrentDirectory

获取应用程序的当前工作目录。

System.IO.Directory.GetCurrentDirectory()

AppDomain.CurrentDomain.BaseDirectory

这两个只能在WindowForm中使用;

Application.StartupPath

Application.ExecutablePath

--获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。

Environment.CurrentDirectory

--获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

Application.ExecutablePath

我在项目文件夹里放了一个mdb数据库,供程序使用,发布时我如何获得该路径?vb里是app.path,就是应用程序的路径,不知在c#里怎么写?

答案一

application.path()

答案二

localpath

具体记不清楚了

答案三 c#的写法 (梓赫)

string path = “”;

if (system.environment.currentdirectory == appdomain.currentdomain.basedirectory)//windows应用程序则相等

…{

path = appdomain.currentdomain.basedirectory;

}

else

…{

path = appdomain.currentdomain.basedirectory + “bin\”;

}

C#获取项目程序路径的方法 1.asp.net webform用“Request.PhysicalApplicationPath获取站点所在虚拟目录的物理路径,最后包含“\”;

2.c# winform用

A:“Application.StartupPath”:获取当前应用程序所在目录的路径,最后不包含“\”;

B:“Application.ExecutablePath ”:获取当前应用程序文件的路径,包含文件的名称;

C:“AppDomain.CurrentDomain.BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“\”;

D:“System.Threading.Thread.GetDomain().BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“\”;

E:“Environment.CurrentDirectory”:获取当前应用程序的路径,最后不包含“\”;

F:“System.IO.Directory.GetCurrentDirectory”:获取当前应用程序的路径,最后不包含“\”;

3.c# windows service用“AppDomain.CurrentDomain.BaseDirectory”或“System.Threading.Thread.GetDomain().BaseDirectory”;

用“Environment.CurrentDirectory”和“System.IO.Directory.GetCurrentDirectory”将得到“ system32”目录的路径;

如果要使用“Application.StartupPath”或“Application.ExecutablePath ”,需要手动添加对“System.Windows.Forms.dll ”的引用,并在程序开头用“using System.Windows.Forms”声明该引用;

C#获取程序当前路径的方法

C#获取程序当前路径的方法

//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。

string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。

string str = System.Environment.CurrentDirectory;

result: X:\xxx\xxx (.exe文件所在的目录)

//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。

string str = System.AppDomain.CurrentDomain.BaseDirectory;

result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)

//获取和设置包含该应用程序的目录的名称。

string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)

//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str = System.Windows.Forms.Application.StartupPath;

result: X:\xxx\xxx (.exe文件所在的目录)

//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string str = System.Windows.Forms.Application.ExecutablePath;

result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//获取应用程序的当前工作目录。

string str = System.IO.Directory.GetCurrentDirectory();

result: X:\xxx\xxx (.exe文件所在的目录)

经常要在应用程序里读取与其在同一个路径下的配置文件或其它文件等,但在Windows平台与WinCE平台下,获取当前路径的方法却不一样,现把本人的经验写在下面,下面的“CurrentPath”属性可以获取应用程序的当前路径,经测试,在WinXP与WinCE下均正常运行。

PS:可以写成静态方法,编译到动态库里,到处都可以用了。

public class Configs

{

private string m_CurrentPath;

private string Platform

{

get

{

return Environment.OSVersion.Platform.ToString();

}



public string CurrentPath



get

}

if(Platform.Equals(“WinCE”))

{

m_CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

}

else if(Platform.Equals(“Win32NT”))

{

m_CurrentPath = Directory.GetCurrentDirectory();

}

return m_CurrentPath;

}

}

public Configs()

{

}

}

C#中获取程序当前路径的集中方法

string str1 =Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。

string str2=Environment.CurrentDirectory;//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。

//备注 按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:\mySubDirectory”)。

string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。

string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。

string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

获取模块的完整路径。

2. System.Environment.CurrentDirectory

获取和设置当前目录(该进程从中启动的目录)的完全限定目录。

3. System.IO.Directory.GetCurrentDirectory()

获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。

4. System.AppDomain.CurrentDomain.BaseDirectory

获取程序的基目录。

5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

获取和设置包括该应用程序的目录的名称。

6. System.Windows.Forms.Application.StartupPath

获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个”\”而已

7. System.Windows.Forms.Application.ExecutablePath

获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: