Path to file?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I'm developing a mini Windows app in Visual Studio, and I'm a bit stuck.

I'm more used to developing web based apps, and whilst still new at that
have stumbled across something I cant seem to do - thus require your most
excellent skills to resolve it for me :o)

I have an xml file in my project and I need to access it when the
application is run, its like a kinda config file...originally I was just
saying load "rules.xml" - but it kept failing, I did a quick search and
found - Application.StartupPath, which I then used like this:

strRulesPath = Application.StartupPath & "\Rules.xml"

The problem with this is that when I'm developing this it dumps the .exe
into the /bin directory, thus my xml file is a level above..

Using asp.net I could do stuff like:

strPath = Request.ApplicationPath("default.aspx")

for example and it worked a charm, I'm guessing there's an equivalent, but
have yet to find it - can anyone give me a hand with this...

Thanks in advance for any help, and welcoming me to this group :o)

Regards

Rob
 
And just to add to that... When you run the program outside of VS it will
be in the same directory as the .exe (not in a subfolder named bin). When
you run your program in the IDE it compiles it and copies it to the bin
directory to execute it.

Should you decide to use an app.config file in your project you'll wee see
this in action. When you run your code in the IDE it will automagically
copy you app.config to the bin folder and rename it to your
<appname>.config.exe.

The bin directory IS the startup path (when in the IDE).

HTH,
Greg
 
Cheers for that guys...

I'll do that for now then, I guess if I wanted the file to reside in a
folder elsewhere, ie install path \ resources\file.xml - then the
'resources' directory would also need to go in to the bin directory too...

Thanks kindly...

Rob
 
* "Rob Meade said:
I have an xml file in my project and I need to access it when the
application is run, its like a kinda config file...originally I was just
saying load "rules.xml" - but it kept failing, I did a quick search and
found - Application.StartupPath, which I then used like this:

strRulesPath = Application.StartupPath & "\Rules.xml"

In addition to the other replies: I strongly recommend to use
'System.IO.Path.Combine' to combine paths toi avoid invalid paths when
the application is started from a drive's root folder.
 
...
In addition to the other replies: I strongly recommend to use
'System.IO.Path.Combine' to combine paths toi avoid invalid paths when
the application is started from a drive's root folder.

Hello Herfried, thanks for the reply.

Could you tell me what that does? It sounds as if it'll take care of any
trailing slash (\) issues for me? Am I right?

Regards

Rob
 
* "Rob Meade said:
Could you tell me what that does? It sounds as if it'll take care of any
trailing slash (\) issues for me? Am I right?

That's what this method will do:

\\\
Dim s As String = System.IO.Path.Combine(Application.StartupPath, "foo.txt")
MsgBox(s)
s = System.IO.Path.Combine("C:\", "foo.txt")
MsgBox(s)
///

If your application is installed in "C:\", then
'Application.StartupPath' will return "C:\", and concatenation with
something that starts with "\" will make the path have two backslashes
after the "C:".

For more information on this method, place the caret over its name and
press the F1 key.
 
...
If your application is installed in "C:\", then
'Application.StartupPath' will return "C:\", and concatenation with
something that starts with "\" will make the path have two backslashes
after the "C:".

For more information on this method, place the caret over its name and
press the F1 key.

Hi Herfried,

Many thanks for the informative reply, I will indeed check this out.

Thanks again.

Regards

Rob
 
Back
Top