Getting File Path

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

    #1

    Getting File Path

    Hello Everyone and thanks for your help in advance. I am
    working on an application that uses as helper class to
    determine if an image file exists. If the file exists,
    the path to the image is returned. Otherwise, a "Picture
    not found" path is returned. I have that part workin
    well. However, I have developed this on a local machine
    and want to push the class to my prosuction server.
    However, the paths change from machine to machine. I
    want to be able to find the fully qualified path to a
    particular file or directory. I tried File.GetFullPat h
    (myFilename), but that returns C:\WINDOWS\syst em32
    \myFilename which is not where the file resides. I want
    to make this as dynamic as possible. Any help would be
    greatly appreciated. Thanks.
  • William Ryan

    #2
    Getting File Path

    If you have a specific number of files, I'd seriously
    consider adding them as embedded resources. To do this,
    go to VS.nET and Add Existing ITem and point to the
    file. This will add it in comiled form to your project.
    Just make sure you set it's build property to Embedded
    Resource. If you do this, it will always exist.

    I'm not sure about the other part though. Why would it
    exist on some machines and not others? Will it/SHould it
    always exist locally or are you going to reference a
    network share for instance. You can't hit a specific
    device name with either FileInfo or Path classes, but I
    think the path class may get what you want ..although if
    you could show me a in depth example I'm sure we can
    figure it out.

    http://msdn.microsoft.com/library/default.asp?
    url=/library/en-
    us/cpref/html/frlrfsystemiopa thclasstopic.as p

    Let me know.

    Bill[color=blue]
    >-----Original Message-----
    >Hello Everyone and thanks for your help in advance. I[/color]
    am[color=blue]
    >working on an application that uses as helper class to
    >determine if an image file exists. If the file exists,
    >the path to the image is returned. Otherwise,[/color]
    a "Picture[color=blue]
    >not found" path is returned. I have that part workin
    >well. However, I have developed this on a local machine
    >and want to push the class to my prosuction server.
    >However, the paths change from machine to machine. I
    >want to be able to find the fully qualified path to a
    >particular file or directory. I tried File.GetFullPat h
    >(myFilename) , but that returns C:\WINDOWS\syst em32
    >\myFilename which is not where the file resides. I want
    >to make this as dynamic as possible. Any help would be
    >greatly appreciated. Thanks.
    >.
    >[/color]

    Comment

    • Hugh McLaughlin

      #3
      Getting File Path

      The reason the paths will be different from machine to
      machine is test machine (laptop) versus production
      webserver. When I use the File.GetFullPat h
      (myFilename), it returns C:\WINDOWS\syst em32
      \myFilename which is not where the file physically
      exists. Why is this?[color=blue]
      >-----Original Message-----
      >If you have a specific number of files, I'd seriously
      >consider adding them as embedded resources. To do this,
      >go to VS.nET and Add Existing ITem and point to the
      >file. This will add it in comiled form to your[/color]
      project.[color=blue]
      >Just make sure you set it's build property to Embedded
      >Resource. If you do this, it will always exist.
      >
      >I'm not sure about the other part though. Why would it
      >exist on some machines and not others? Will it/SHould[/color]
      it[color=blue]
      >always exist locally or are you going to reference a
      >network share for instance. You can't hit a specific
      >device name with either FileInfo or Path classes, but I
      >think the path class may get what you want ..although if
      >you could show me a in depth example I'm sure we can
      >figure it out.
      >
      >http://msdn.microsoft.com/library/default.asp?
      >url=/library/en-
      >us/cpref/html/frlrfsystemiopa thclasstopic.as p
      >
      >Let me know.
      >
      >Bill[color=green]
      >>-----Original Message-----
      >>Hello Everyone and thanks for your help in advance. I[/color]
      >am[color=green]
      >>working on an application that uses as helper class to
      >>determine if an image file exists. If the file exists,
      >>the path to the image is returned. Otherwise,[/color]
      >a "Picture[color=green]
      >>not found" path is returned. I have that part workin
      >>well. However, I have developed this on a local machine
      >>and want to push the class to my prosuction server.
      >>However, the paths change from machine to machine. I
      >>want to be able to find the fully qualified path to a
      >>particular file or directory. I tried File.GetFullPat h
      >>(myFilename ), but that returns C:\WINDOWS\syst em32
      >>\myFilename which is not where the file resides. I[/color][/color]
      want[color=blue][color=green]
      >>to make this as dynamic as possible. Any help would be
      >>greatly appreciated. Thanks.
      >>.
      >>[/color]
      >.
      >[/color]

      Comment

      • Fergus Cooney

        #4
        Re: Getting File Path

        Hi Hugh,

        You said:
        || I tried File.GetFullPat h (myFilename), but that returns
        || "C:\WINDOWS\sys tem32\myFilenam e" which is not where the file
        resides

        There isn't a File.GetFullPat h(). But there is Path.GetFullPat h() which
        creates a path using the current directory. Which is not where the file
        resides!

        You need to obtain the FileInfo for "myFilename " and use
        oFileInfo.FullP ath().

        Obtaining the FileInfo is left as an exercise.

        Regards,
        Fergus


        Comment

        • Marc Scheuner [MVP ADSI]

          #5
          Re: Getting File Path

          >Hello Everyone and thanks for your help in advance. I am[color=blue]
          >working on an application that uses as helper class to
          >determine if an image file exists. If the file exists,
          >the path to the image is returned. Otherwise, a "Picture
          >not found" path is returned.[/color]

          How about this:

          public string GetImagePath(st ring aImageFileName)
          {
          if(File.Exists( aImageFileName)
          {
          return Path.GetDirecto ryName(aImageFi leName)
          }
          else
          {
          return "Picture not found"
          }
          }

          The "Path" class is in the System.IO namespace.

          Marc
          =============== =============== =============== =============== ====
          Marc Scheuner May The Source Be With You!
          Bern, Switzerland m.scheuner(at)i nova.ch

          Comment

          • Dave

            #6
            Re: Getting File Path

            If the file is supposed to be in the same directory as the application, then
            you could do something like this.

            string path = Assembly.GetExe cutingAssembly( ).Location;
            string fullName = Path.Combine(pa th,imageFile);
            if ( File.Exists(ful lName) )
            {
            }

            "Hugh McLaughlin" <hugh@kmcnetwor k.com> wrote in message
            news:008201c369 a8$6e41e3e0$a60 1280a@phx.gbl.. .[color=blue]
            > Hello Everyone and thanks for your help in advance. I am
            > working on an application that uses as helper class to
            > determine if an image file exists. If the file exists,
            > the path to the image is returned. Otherwise, a "Picture
            > not found" path is returned. I have that part workin
            > well. However, I have developed this on a local machine
            > and want to push the class to my prosuction server.
            > However, the paths change from machine to machine. I
            > want to be able to find the fully qualified path to a
            > particular file or directory. I tried File.GetFullPat h
            > (myFilename), but that returns C:\WINDOWS\syst em32
            > \myFilename which is not where the file resides. I want
            > to make this as dynamic as possible. Any help would be
            > greatly appreciated. Thanks.[/color]


            Comment

            Working...