Environment.CurrentDirectory in C#.NET

风流意气都作罢 提交于 2019-12-22 05:03:29

问题


The property Environment.CurrentDirectory always returns the path of system directory instead my application directory. In my colleague's PC, it returns application directory.

What is the problem? How can I solve it?

The following code is working for me

ePCRSettings = XMLParser.XmlParser.Deserialize<PCRGeneratorSettings>(string.Format("{0}\\ePCRPDFSettings.xml", AppDomain.CurrentDomain.BaseDirectory));

AppDomain.CurrentDomain.BaseDirectory - Returns the directory E:\MyApplications\.

The following code is not working for me

ePCRSettings = XMLParser.XmlParser.Deserialize<PCRGeneratorSettings>(string.Format("{0}\\ePCRPDFSettings.xml", Environment.CurrentDirectory));

Environment.CurrentDirectory - Returns c:\windows\system32.

This .dll file can be used in VB 6 and ASP.NET applications


回答1:


set current directory

Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); //or set executing Assembly location path in param

Environment.CurrentDirectory //now returns your app path



回答2:


Use

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);




回答3:


You shouldn't be using the Environment.CurrentDirectory value as a base for file lookups because it can change and may not always be under your control. e.g. a File Save As to a different folder may change the 'current folder' value. As you can see it can yield unpredictable results.

Use a value that you can control better. e.g. a ResourcesFolderPath value in a configuration (xml?) file that is updated when you install your app.




回答4:


I suspect that this could have something to do with the current user id that the app is running under, for example if you are running the app in a user session (e.g. debugging in VS) then this may return your current directory, but if you were running it under IIS then this could be why it is defaulting to the system folder?



来源:https://stackoverflow.com/questions/1321628/environment-currentdirectory-in-c-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!