Hi, I have a windows service and I need to create directory to store some info. The directory path must be relative to the windows service exe file. How can get this exe file path ?
It works. Thank you.
2010-05-14 12:17:26
+2
A:
Instead of using a directory relative to the executable, and therefore needing admin privileges, why not use the common application data directory, which is accessible through
Environment.GetFolderPath(SpecialFolder.CommonApplicationData)
This way your app doesn't need write access to its own install directory, which makes you more secure.
Stewart
2010-05-14 12:13:17
Also, in Windows Vista and Windows 7, you can't write to the program files folder, even as an administrator!
Chris Dunaway
2010-05-14 15:50:19
A:
m_baseDir = AppDomain.CurrentDomain.BaseDirectory +
AppDomain.CurrentDomain.RelativeSearchPath;
hope this will help you
Pranay Rana
2010-05-14 12:15:21
A:
The default directory for a windows service is the System32 folder. In your service, though, you can change the current directory to the directory that you specified in the service installation by doing the following in your OnStart:
// Define working directory (For a service, this is set to System)
// This will allow us to reference the app.config if it is in the same directory as the exe
Process pc = Process.GetCurrentProcess();
Directory.SetCurrentDirectory(pc.MainModule.FileName.Substring(0, pc.MainModule.FileName.LastIndexOf(@"\")));
Edit: an even simpler method (but I haven't tested yet):
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);
derek
2010-05-14 12:19:15
A:
string path = Process.GetCurrentProcess().MainModule.FileName;
path = path.Substring(0, path.LastIndexOf("\\"));
svchost.exe is the executable which runs your service which is in system32. Hence we need to get to the module which is being run by the process.
Sachin
2010-05-14 12:58:51