Generic path description - please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • almurph@altavista.com

    #1

    Generic path description - please help

    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:\fo lderA\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.

  • Ken Tucker [MVP]

    #2
    Re: Generic path description - please help

    Hi,



    Ken
    ------------
    <almurph@altavi sta.com> wrote in message
    news:1143545405 .051824.277070@ v46g2000cwv.goo glegroups.com.. .[color=blue]
    > 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:\fo lderA\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.
    >[/color]


    Comment

    • Cerebrus

      #3
      Re: Generic path description - please help

      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 Canonicalizatio n 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(myX mlPath)
      doc.Load(exactP ath)
      :
      :
      Catch ex as Exception
      :
      End Try
      ------------------------------------------
      HTH,

      Regards,

      Cerebrus

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Generic path description - please help

        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> schrieb:[color=blue]
        > http://www.vb-tips.com/default.aspx?...c-dfa89d0d9842[/color]

        In addition to that, you may want to use 'GetExecutingAs sembly' instead of
        'GetEntryAssemb ly' in order to get the path to the DLL containing the call
        to the method.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • almurph@altavista.com

          #5
          Re: Generic path description - please help

          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\mic rosoft.net\fram ework\v1.4322\e tc...


          Confused,
          Al

          Comment

          Working...