Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In windows applications , we mention the path as Application.StartUpPath.....
In the same way how to mention the path in Windows Services...
Posted

First, don't mix up start-up path and executable path. First thing is absolutely unreliable and should never be relied upon. For interactive applications, the user can start application from anywhere. For services… I never even though of checking it up, but also might vary. Executable path is reliable thing, only remember that you can put read-only object there (or in some sub-directory).

There are different ways to do it, but the result sometimes depends on how application is hosted, so they fail to show correct path for a service or, say, application hosted by Visual Studio. Here is the portable reliable way of finding the executable path which I use in all cases:

C#
string exeDirectory =
   System.IO.GetDirectoryName(
      System.Reflection.Assembly.GetEntryAssembly().Location);


—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 3-Oct-11 19:18pm    
5'ed! This is usually the case, unless you are running in a customized AppDomain, and this isn't the case for Windows Service applications.
not sure if this is what you need because services are quite different from apps but
AppDomain.CurrentDomain.BaseDirectory
might be what your looking for?
 
Share this answer
 
Comments
sunil0518 30-Sep-11 0:07am    
Actually I am mentioning some xml files in service... So I want to find the path of the folder in the windows service
killabyte 30-Sep-11 0:48am    
well if your using a FileSystemWatcher then you can just reference the path through that object?
Sergey Alexandrovich Kryukov 30-Sep-11 1:10am    
Not sure. What is different Application Domains are used? Did you try it?
Really universal path the the location of EntryAssembly, please see my solution which I tested with different way of hosting application including services (hosted by Service Controller).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900