locating a service's path in visual studio

I'm creating a service in VS2012.
When my service starts, I need it to read a config file located in the same folder where the EXE to the service is located.
Currently it's looking for it in windows\system32.
How can I dynamically reference the folder location that the service exe is launched from?
LVL 67
sirbountyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
You can get it using the BaseDirectory property of the AppDomain:
string currentPath = System.AppDomain.CurrentDomain.BaseDirectory;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sirbountyAuthor Commented:
Thanks Carl - before I rebuild and deploy, do you know if that includes the trailing backslash or do I need to add it? (c:\folder\service.exe")
0
it_saigeDeveloperCommented:
*No Points*

The returned path does include the trailing backslash.  Proof of concept:
using System;

namespace EE_Q28704236
{
	class Program
	{
		static void Main(string[] args)
		{
			Console.WriteLine("Current application directory: {0}", System.AppDomain.CurrentDomain.BaseDirectory);
			Console.ReadLine();
		}
	}
}

Open in new window

Which produces the following output -Capture.JPG-saige-
1
Jacques Bourgeois (James Burger)PresidentCommented:
The best way to work with paths, when you do not know if the backslash is there or not, or if you want to be sure that you will never be fooled by methods that eventually change the way they return a path is to use the System.IO.Path.Combine method:

System.IO.Path.Combine (folderName, "service.exe")

It takes care of adding the backslash if needed.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.