Generic path description - please help

  • Thread starter Thread starter almurph
  • Start date Start date
A

almurph

Hi,

I have a VB.NET class library and I have to access a xml file that
will always be in the same directory as the class library.

So I use the followign code to open the file:

Dim doc as XmlDocument
doc.Load("C:\folderA\folderB\a.xml")

Thing is though this path is subject to change. Is there any generic
way to say that a.xml will always be in the same folder as this? Do
you know what I mean?

Any comments/suggestions/code-samples much appreciated.

Al.
 
Hi,

Ken's solution is very robust and foolproof, but if High security is
not your priority, you can also use relative paths, like :

Since the xml file is in the same directory as the class library,
------------------------------------------
doc.Load("a.xml")
------------------------------------------

However this has many disadvantages from the Canonicalization point of
view. So to make it more robust, we can use :
------------------------------------------
Dim myXmlPath as string = "a.xml"
Try
Dim exactPath as string = System.IO.Path.GetFullPath(myXmlPath)
doc.Load(exactPath)
:
:
Catch ex as Exception
:
End Try
------------------------------------------
HTH,

Regards,

Cerebrus
 
Thanks everyone - problem is though I keep on getting the wrong
directory. Depending on what technique is use I either get:

C:\windows

or

C:\windoiws\microsoft.net\framework\v1.4322\etc...


Confused,
Al
 
Back
Top