469,575 Members | 1,613 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,575 developers. It's quick & easy.

Physical vs Logical My Documents question

dgk
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.
Nov 21 '05 #1
9 2334
dgk wrote:
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.


HKCU\Software\Microsoft\Windows\CurrentVersion\Exp lorer\Shell Folders
That contains the location for all the Special folders for that user.

HTH,
Tibby
Nov 21 '05 #2
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
Nov 21 '05 #3
Hi,

Environment.GetFolderPath(Environment.SpecialFolde r.Personal)
returns the path to My Documents.

Ken
-----------------
"dgk" <so******************@zero-spam-hotmail.com> wrote in message
news:6m********************************@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.
Nov 21 '05 #4
dgk
On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:
Hi,

Environment.GetFolderPath(Environment.SpecialFolde r.Personal)
returns the path to My Documents.

Ken
-----------------
"dgk" <so******************@zero-spam-hotmail.com> wrote in message
news:6m********************************@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\ProductX",
where ProductX is the name of my program. When FolderBrowserDialog 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.

Nov 21 '05 #5
DGK,

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

Cor
Nov 21 '05 #6
dgk,
In addition to the other comments.

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

The Uri.MakeRelative 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.IsPathRooted(sourcePath) Then
source = New Uri(sourcePath)
Else
source = New Uri(root, sourcePath)
End If
Dim relative As String = root.MakeRelative(source)
Return Not Path.IsPathRooted(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.AbsolutePath. 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.IsPathRooted(sourcePath) Then
source = New Uri(sourcePath)
Else
source = New Uri(root, sourcePath)
End If
Return source.AbsolutePath.StartsWith(root.AbsolutePath)
End Function
Dim myDocuments As String =
Environment.GetFolderPath(Environment.SpecialFolde r.Personal) & "\"c
Dim path1 As String = "D:\Documents and Settings\Jay\My
Documents\Visual Studio Projects"
Dim path2 As String = "D:\Documents and Settings\dgk\My
Documents\Visual Studio Projects"
Dim path3 As String = "D:\Documents and Settings\Jay\..\Jay\My
Documents\Visual Studio Projects"
Dim path4 As String = "Visual Studio Projects"

Debug.WriteLine(myDocuments, "My Documents")
Debug.WriteLine(IsRoot(myDocuments, path1), path1)
Debug.WriteLine(IsRoot(myDocuments, path2), path2)
Debug.WriteLine(IsRoot(myDocuments, path3), path3)
Debug.WriteLine(IsRoot(myDocuments, path4), path4)
Hope this helps
Jay
"dgk" <so******************@zero-spam-hotmail.com> wrote in message
news:6m********************************@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.
Nov 21 '05 #7
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.MakeRelative will do that!

Try something like:

Dim myDocuments As New
Uri(Environment.GetFolderPath(Environment.SpecialF older.Personal), True)
Dim dialog As New SaveFileDialog
If dialog.ShowDialog() = DialogResult.OK Then
Dim toUri As New Uri(dialog.FileName, True)
MessageBox.Show(myDocuments.MakeRelative(toUri), "Safe As")
End If

Hope this helps
Jay

"dgk" <so******************@hot-nospamp-mail.com> wrote in message
news:pm********************************@4ax.com...
| On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
| <vb***@bellsouth.net> wrote:
|
| >Hi,
| >
| > Environment.GetFolderPath(Environment.SpecialFolde r.Personal)
| >returns the path to My Documents.
| >
| >Ken
| >-----------------
| >"dgk" <so******************@zero-spam-hotmail.com> wrote in message
| >news:6m********************************@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\ProductX",
| where ProductX is the name of my program. When FolderBrowserDialog 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.
|
Nov 21 '05 #8
Doh!!

Here is a sample using the FolderBrowserDialog:

Dim dialog As New FolderBrowserDialog
If dialog.ShowDialog() = DialogResult.OK Then
Dim rootUri As New
Uri(Environment.GetFolderPath(dialog.RootFolder), True)
Dim toUri As New Uri(dialog.SelectedPath, True)
MessageBox.Show(rootUri.MakeRelative(toUri), "Safe As")
End If

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

Hope this helps
Jay

"dgk" <so******************@hot-nospamp-mail.com> wrote in message
news:pm********************************@4ax.com...
| On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
| <vb***@bellsouth.net> wrote:
|
| >Hi,
| >
| > Environment.GetFolderPath(Environment.SpecialFolde r.Personal)
| >returns the path to My Documents.
| >
| >Ken
| >-----------------
| >"dgk" <so******************@zero-spam-hotmail.com> wrote in message
| >news:6m********************************@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\ProductX",
| where ProductX is the name of my program. When FolderBrowserDialog 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.
|
Nov 21 '05 #9
dgk
On Tue, 2 Aug 2005 13:13:12 -0500, "Jay B. Harlow [MVP - Outlook]"
<Ja************@msn.com> wrote:
dgk,
In addition to the other comments.

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

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.
Nov 21 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by chris6995 | last post: by
1 post views Thread by MattM | last post: by
1 post views Thread by Jose Barragan | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.