Physical vs Logical My Documents question

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

    #1

    Physical vs Logical My Documents question

    Is there some built in way to know whether a physical folder path is
    'My Documents" for a specific user? I can always use xxx.StartsWith to
    compare it to the enumeration returned by the Personal folder, but it
    seems a bit inelegant.
  • Tibby

    #2
    Re: Physical vs Logical My Documents question

    dgk wrote:
    [color=blue]
    > Is there some built in way to know whether a physical folder path is
    > 'My Documents" for a specific user? I can always use xxx.StartsWith to
    > compare it to the enumeration returned by the Personal folder, but it
    > seems a bit inelegant.[/color]

    HKCU\Software\M icrosoft\Window s\CurrentVersio n\Explorer\Shel l Folders
    That contains the location for all the Special folders for that user.

    HTH,
    Tibby

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Physical vs Logical My Documents question

      DGK,

      Although I am not clear about what you write, because you use your own
      probably your own mnemonics of classes.

      I assume that you with the Enum means the enum in the Environment class and
      not an own build Enum.

      Than it seems for me in combination with the path class, from what I think
      you mean with xxx and the method to get the full path in that what I think
      you mean with Start, a complete normal method that you use.

      I hope this helps,

      Cor


      Comment

      • Ken Tucker [MVP]

        #4
        Re: Physical vs Logical My Documents question

        Hi,

        Environment.Get FolderPath(Envi ronment.Special Folder.Personal )
        returns the path to My Documents.

        Ken
        -----------------
        "dgk" <sonicechoes-spamless@zero-spam-hotmail.com> wrote in message
        news:6mdte1hqh8 428sugce65fs5u3 u7sbguq32@4ax.c om...
        Is there some built in way to know whether a physical folder path is
        'My Documents" for a specific user? I can always use xxx.StartsWith to
        compare it to the enumeration returned by the Personal folder, but it
        seems a bit inelegant.


        Comment

        • dgk

          #5
          Re: Physical vs Logical My Documents question

          On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
          <vb2ae@bellsout h.net> wrote:
          [color=blue]
          >Hi,
          >
          > Environment.Get FolderPath(Envi ronment.Special Folder.Personal )
          >returns the path to My Documents.
          >
          >Ken
          >-----------------
          >"dgk" <sonicechoes-spamless@zero-spam-hotmail.com> wrote in message
          >news:6mdte1hqh 8428sugce65fs5u 3u7sbguq32@4ax. com...
          >Is there some built in way to know whether a physical folder path is
          >'My Documents" for a specific user? I can always use xxx.StartsWith to
          >compare it to the enumeration returned by the Personal folder, but it
          >seems a bit inelegant.
          >[/color]

          Sorry, I should be more specific. I have the user pick the folder
          where a database will be kept, defaulting to "My Documents\Produ ctX",
          where ProductX is the name of my program. When FolderBrowserDi alog is
          used to have the user pick a folder, and they choose "My Documents",
          it returns the physical address as the SelectedPath, not "My
          Documents" which is what the user expects to see. So I don't want to
          subsequently show the physical location, I want to show "My
          Documents".

          I check to see if the physical path returned begins with the physical
          My Documents path, and then show it as My Documents to the user.
          Nothing major, I've already written the trivial code to handle it but
          it just seemed that there would be a built in function (oops, Shared
          Class method) that would deal with the physical vs logical
          implementation of the Special Folders.

          Comment

          • Cor Ligthert [MVP]

            #6
            Re: Physical vs Logical My Documents question

            DGK,

            I have a routine that does it the same way as you do.

            Cor


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: Physical vs Logical My Documents question

              dgk,
              In addition to the other comments.

              I was thinking that the System.Uri.Make Relative function might help. Which
              is not specifically what you want, but may give you an indication of what
              you want...

              The Uri.MakeRelativ e function "determines the difference between two Uri
              instances". There is a constructor on Uri that takes a root URI and
              "appends" a relative path.

              The System.IO.Path. IsPathRooted can be used to determine if the path itself
              is relative or absolute.

              One of the advantages of using the Uri object in your function is that it
              removes superfluous segments, such as "/" and resolves ".." in the path.

              One could create a function similar to:

              Public Function IsRoot(ByVal rootPath As String, ByVal sourcePath As
              String) As Boolean
              Dim root As New Uri(rootPath)
              Dim source As Uri
              If Path.IsPathRoot ed(sourcePath) Then
              source = New Uri(sourcePath)
              Else
              source = New Uri(root, sourcePath)
              End If
              Dim relative As String = root.MakeRelati ve(source)
              Return Not Path.IsPathRoot ed(relative)
              End Function

              Unfortunately it doesn't work, per se.


              Going back to your original idea, I would modify the above to use StartWith
              on Uri.AbsolutePat h. Something like:

              Public Function IsRoot(ByVal rootPath As String, ByVal sourcePath As
              String) As Boolean
              Dim root As New Uri(rootPath & "\"c)
              Dim source As Uri
              If Path.IsPathRoot ed(sourcePath) Then
              source = New Uri(sourcePath)
              Else
              source = New Uri(root, sourcePath)
              End If
              Return source.Absolute Path.StartsWith (root.AbsoluteP ath)
              End Function


              Dim myDocuments As String =
              Environment.Get FolderPath(Envi ronment.Special Folder.Personal ) & "\"c
              Dim path1 As String = "D:\Documen ts and Settings\Jay\My
              Documents\Visua l Studio Projects"
              Dim path2 As String = "D:\Documen ts and Settings\dgk\My
              Documents\Visua l Studio Projects"
              Dim path3 As String = "D:\Documen ts and Settings\Jay\.. \Jay\My
              Documents\Visua l Studio Projects"
              Dim path4 As String = "Visual Studio Projects"

              Debug.WriteLine (myDocuments, "My Documents")
              Debug.WriteLine (IsRoot(myDocum ents, path1), path1)
              Debug.WriteLine (IsRoot(myDocum ents, path2), path2)
              Debug.WriteLine (IsRoot(myDocum ents, path3), path3)
              Debug.WriteLine (IsRoot(myDocum ents, path4), path4)


              Hope this helps
              Jay


              "dgk" <sonicechoes-spamless@zero-spam-hotmail.com> wrote in message
              news:6mdte1hqh8 428sugce65fs5u3 u7sbguq32@4ax.c om...
              | Is there some built in way to know whether a physical folder path is
              | 'My Documents" for a specific user? I can always use xxx.StartsWith to
              | compare it to the enumeration returned by the Personal folder, but it
              | seems a bit inelegant.


              Comment

              • Jay B. Harlow [MVP - Outlook]

                #8
                Re: Physical vs Logical My Documents question

                dgk,
                | I check to see if the physical path returned begins with the physical
                | My Documents path, and then show it as My Documents to the user.
                | Nothing major, I've already written the trivial code to handle it but
                Uri.MakeRelativ e will do that!

                Try something like:

                Dim myDocuments As New
                Uri(Environment .GetFolderPath( Environment.Spe cialFolder.Pers onal), True)
                Dim dialog As New SaveFileDialog
                If dialog.ShowDial og() = DialogResult.OK Then
                Dim toUri As New Uri(dialog.File Name, True)
                MessageBox.Show (myDocuments.Ma keRelative(toUr i), "Safe As")
                End If

                Hope this helps
                Jay



                "dgk" <sonicechoes-spamless@hot-nospamp-mail.com> wrote in message
                news:pmrue192oo dh4v9upupi42386 8dacs9vsl@4ax.c om...
                | On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
                | <vb2ae@bellsout h.net> wrote:
                |
                | >Hi,
                | >
                | > Environment.Get FolderPath(Envi ronment.Special Folder.Personal )
                | >returns the path to My Documents.
                | >
                | >Ken
                | >-----------------
                | >"dgk" <sonicechoes-spamless@zero-spam-hotmail.com> wrote in message
                | >news:6mdte1hqh 8428sugce65fs5u 3u7sbguq32@4ax. com...
                | >Is there some built in way to know whether a physical folder path is
                | >'My Documents" for a specific user? I can always use xxx.StartsWith to
                | >compare it to the enumeration returned by the Personal folder, but it
                | >seems a bit inelegant.
                | >
                |
                | Sorry, I should be more specific. I have the user pick the folder
                | where a database will be kept, defaulting to "My Documents\Produ ctX",
                | where ProductX is the name of my program. When FolderBrowserDi alog is
                | used to have the user pick a folder, and they choose "My Documents",
                | it returns the physical address as the SelectedPath, not "My
                | Documents" which is what the user expects to see. So I don't want to
                | subsequently show the physical location, I want to show "My
                | Documents".
                |
                | I check to see if the physical path returned begins with the physical
                | My Documents path, and then show it as My Documents to the user.
                | Nothing major, I've already written the trivial code to handle it but
                | it just seemed that there would be a built in function (oops, Shared
                | Class method) that would deal with the physical vs logical
                | implementation of the Special Folders.
                |


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: Physical vs Logical My Documents question

                  Doh!!

                  Here is a sample using the FolderBrowserDi alog:

                  Dim dialog As New FolderBrowserDi alog
                  If dialog.ShowDial og() = DialogResult.OK Then
                  Dim rootUri As New
                  Uri(Environment .GetFolderPath( dialog.RootFold er), True)
                  Dim toUri As New Uri(dialog.Sele ctedPath, True)
                  MessageBox.Show (rootUri.MakeRe lative(toUri), "Safe As")
                  End If

                  Notice that I use the FolderBrowserDi alog.RootFolder property to determine
                  the root path to make the relative path from.

                  Hope this helps
                  Jay

                  "dgk" <sonicechoes-spamless@hot-nospamp-mail.com> wrote in message
                  news:pmrue192oo dh4v9upupi42386 8dacs9vsl@4ax.c om...
                  | On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
                  | <vb2ae@bellsout h.net> wrote:
                  |
                  | >Hi,
                  | >
                  | > Environment.Get FolderPath(Envi ronment.Special Folder.Personal )
                  | >returns the path to My Documents.
                  | >
                  | >Ken
                  | >-----------------
                  | >"dgk" <sonicechoes-spamless@zero-spam-hotmail.com> wrote in message
                  | >news:6mdte1hqh 8428sugce65fs5u 3u7sbguq32@4ax. com...
                  | >Is there some built in way to know whether a physical folder path is
                  | >'My Documents" for a specific user? I can always use xxx.StartsWith to
                  | >compare it to the enumeration returned by the Personal folder, but it
                  | >seems a bit inelegant.
                  | >
                  |
                  | Sorry, I should be more specific. I have the user pick the folder
                  | where a database will be kept, defaulting to "My Documents\Produ ctX",
                  | where ProductX is the name of my program. When FolderBrowserDi alog is
                  | used to have the user pick a folder, and they choose "My Documents",
                  | it returns the physical address as the SelectedPath, not "My
                  | Documents" which is what the user expects to see. So I don't want to
                  | subsequently show the physical location, I want to show "My
                  | Documents".
                  |
                  | I check to see if the physical path returned begins with the physical
                  | My Documents path, and then show it as My Documents to the user.
                  | Nothing major, I've already written the trivial code to handle it but
                  | it just seemed that there would be a built in function (oops, Shared
                  | Class method) that would deal with the physical vs logical
                  | implementation of the Special Folders.
                  |


                  Comment

                  • dgk

                    #10
                    Re: Physical vs Logical My Documents question

                    On Tue, 2 Aug 2005 13:13:12 -0500, "Jay B. Harlow [MVP - Outlook]"
                    <Jay_Harlow_MVP @msn.com> wrote:
                    [color=blue]
                    >dgk,
                    >In addition to the other comments.
                    >
                    >I was thinking that the System.Uri.Make Relative function might help. Which
                    >is not specifically what you want, but may give you an indication of what
                    >you want...
                    >...[/color]


                    OUCH! And I was thinking that my solution wasn't elegant. But this is
                    interesting stuff and I will investigate it. I hadn't thought about
                    the .. problem. I didn't realize that that could ever be returned by a
                    function. Always more to learn. Add URI to the list.

                    Comment

                    Working...