I have a simple windows form app that I need to get the file path for. I am placing a config file in the same directory and I need to be able to get the path to that file.
I have used
Application.CommonAppDataPath
but that returns the path with 1.0.0.0 at the end.
and
Applicaiton.StartupPath
but that returns the path with \bin\debug at the end
Is there anyway to get the path to just the main file directory without anything appended to the end?
Application.StartupPathis returning the path with\bin\debugon the end because that is where your code is running, at least on your development machine.If you are going to deploy this away from your development machine then
Application.StartupPathwill give you what you’re asking for – the file path for your application. And yes, if you have deployed the config file to that same location, your code is going to find it.How to get the application also working on your development machine and get round the
bin\debugissue? Well, a dirty hack would be just to chop thebin\debugstring off the end ofApplication.StartupPath.In that case, if you need to check for whether you’re running inside the debugger, see this question