Opening a file from application location

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • grant

    Opening a file from application location

    Good day,

    How do I open a text file from the applications install location?
    I am currently opening a text file using "StreamRead er sr = new
    StreamReader("D B.txt");" and then reading from that file.

    Problem is if I create a shortcut on the users desktop it tries to read that
    file from the desktop when I need it to read from the actual location of the
    executable.

    Thanks,
    Grant


  • Peter Duniho

    #2
    Re: Opening a file from application location

    On Thu, 13 Nov 2008 23:24:19 -0800, grant <gpsnett@hotmai l.comwrote:
    Good day,
    >
    How do I open a text file from the applications install location?
    I am currently opening a text file using "StreamRead er sr = new
    StreamReader("D B.txt");" and then reading from that file.
    >
    Problem is if I create a shortcut on the users desktop it tries to read
    that
    file from the desktop when I need it to read from the actual location of
    the
    executable.
    You can get path information for the executable from the assembly. See
    the Assembly class for details.

    That said, if you have control over the install process (and why wouldn't
    you? :) ), it seems to me that you may be better off storing a registry
    key value specifying the file's path (and maybe even its name) during the
    installation and retrieving that information at run-time. That would work
    just as well, and would generalize the code so that fewer changes have to
    be made to the code in the future if the location of the file changes.

    Pete

    Comment

    • Peter Morris

      #3
      Re: Opening a file from application location

      The name of the file "DB.txt" implies to me that you are wish to write to
      this file, am I correct?




      --
      Pete
      ====



      Comment

      • grant

        #4
        Re: Opening a file from application location

        Awesome, thanks.

        "Peter Duniho" <NpOeStPeAdM@nn owslpianmk.comw rote in message
        news:op.ukllxki 08jd0ej@petes-computer.local. ..
        On Thu, 13 Nov 2008 23:24:19 -0800, grant <gpsnett@hotmai l.comwrote:
        >
        >Good day,
        >>
        >How do I open a text file from the applications install location?
        >I am currently opening a text file using "StreamRead er sr = new
        >StreamReader(" DB.txt");" and then reading from that file.
        >>
        >Problem is if I create a shortcut on the users desktop it tries to read
        >that
        >file from the desktop when I need it to read from the actual location of
        >the
        >executable.
        >
        You can get path information for the executable from the assembly. See
        the Assembly class for details.
        >
        That said, if you have control over the install process (and why wouldn't
        you? :) ), it seems to me that you may be better off storing a registry
        key value specifying the file's path (and maybe even its name) during the
        installation and retrieving that information at run-time. That would work
        just as well, and would generalize the code so that fewer changes have to
        be made to the code in the future if the location of the file changes.
        >
        Pete

        Comment

        Working...