.NETGURU
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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification