Environment.CurrentDirectory in C # .NET

The Environment.CurrentDirectory property always returns the path to the system directory instead of my application directory. In my PC counterpart, it returns the application directory.

What is the problem? How can I solve it?

The following code works 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 does not work 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.

+6
source share
4 answers

set current directory

 Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); //or set executing Assembly location path in param Environment.CurrentDirectory //now returns your app path 
+10
source

Use

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

+5
source

You should not use the value of Environment.CurrentDirectory as a base for finding files, because it can change and may not always be under your control. for example, a Save As file in another folder may change the value of the current folder. As you can see, this can give unpredictable results.

Use a value that you can control better. for example, the value of ResourceFolderPath in the configuration file (xml?), which is updated when you install your application.

+3
source

I suspect that this may have something to do with the current identifier of the user the application is working with, for example, if you use the application in a user session (for example, debug in VS), then this may return your current directory, but if you ran it under IIS, then this may be the reason that it defaults to the system folder?

+1
source

All Articles