Path to file?
Messages   Related Types
This message was discovered on microsoft.public.dotnet.languages.vb.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

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

Reply to this message...
 
    
Herfried K. Wagner [MVP] (VIP)
* "Rob Meade" <Click here to reveal e-mail address> scripsit:
[Original message clipped]

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Reply to this message...
 
    
Rob Meade
"Herfried K. Wagner [MVP]" wrote ...

[Original message clipped]

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

Reply to this message...
 
    
Herfried K. Wagner [MVP] (VIP)
* "Rob Meade" <Click here to reveal e-mail address> scripsit:
[Original message clipped]

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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Reply to this message...
 
    
Rob Meade
"Herfried K. Wagner [MVP]" wrote ...

[Original message clipped]

Hi Herfried,

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

Thanks again.

Regards

Rob

Reply to this message...
 
    
Jared
I just put a copy of the file in the bin directory.

"Rob Meade" <Click here to reveal e-mail address> wrote in message
news:pqI0d.5154$3%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Greg Burns
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

"Jared" <@Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Rob Meade
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

Reply to this message...
 
 
System.IO.Path
System.Windows.Forms.Application




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com