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\,ææ¶ä¸ä¸å®è¿åä»ä¹ä¸ä¸ï¼è¿æ¯ä»»ä½åºç¨ç¨åºæåä¸æ¬¡æä½è¿çç®å½ï¼æ¯å¦ä½ ç¨Wordæå¼äºE:\doc\my.docè¿ä¸ªæ件ï¼æ¤æ¶æ§è¡è¿ä¸ªæ¹æ³å°±è¿åäºE:\docäºã
4. System.AppDomain.CurrentDomain.BaseDirectory
è·åç¨åºçåºç®å½ã
5. System.Windows.Forms.Application.StartupPath
è·åå¯å¨äºåºç¨ç¨åºçå¯æ§è¡æ件çè·¯å¾ãææå2ã5ä¸æ ·ãåªæ¯5è¿åçå符串åé¢å¤äºä¸ä¸ª"\"èå·²ã
6. System.Windows.Forms.Application.ExecutablePath
è·åå¯å¨äºåºç¨ç¨åºçå¯æ§è¡æ件çè·¯å¾åæ件åï¼ææå1ä¸æ ·ã
7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
è·åå设置å
æ¬è¯¥åºç¨ç¨åºçç®å½çå称ã
äºãæä½ç¯å¢åé
å©ç¨System.Environment.GetEnvironmentVariable()æ¹æ³å¯ä»¥å¾æ¹ä¾¿å°åå¾ç³»ç»ç¯å¢åéï¼å¦ï¼System.Environment.GetEnvironmentVariable("windir")å°±å¯ä»¥åå¾windowsç³»ç»ç®å½çè·¯å¾ã
以ä¸æ¯ä¸äºå¸¸ç¨çç¯å¢åéåå¼ï¼
System.Environment.GetEnvironmentVariable("windir");
System.Environment.GetEnvironmentVariable("INCLUDE");
System.Environment.GetEnvironmentVariable("TMP");
System.Environment.GetEnvironmentVariable("TEMP");
System.Environment.GetEnvironmentVariable("Path");
ä¸ãåºç¨å®ä¾
ç¼åäºä¸ä¸ªWinFormç¨åºï¼é¡¹ç®æ件åæ¾äºD:\Projects\Demoï¼ç¼è¯åçæ件ä½äºD:\Projects\Demo\bin\Debugï¼æåçç»æå¦ä¸ï¼
1ãSystem.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe
2ãSystem.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug
3ãSystem.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug
4ãSystem.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\
5ãSystem.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\
6ãSystem.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug
7ãSystem.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
温馨提示:答案为网友推荐,仅供参考