Physical vs Logical My Documents question

  • Thread starter Thread starter dgk
  • Start date Start date
D

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.
 
dgk said:
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\Explorer\Shell Folders
That contains the location for all the Special folders for that user.

HTH,
Tibby
 
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
 
Hi,

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

Ken
 
Hi,

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

Ken
-----------------
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.
 
DGK,

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

Cor
 
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.SpecialFolder.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


| 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.
 
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.SpecialFolder.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



| On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
|
| >Hi,
| >
| > Environment.GetFolderPath(Environment.SpecialFolder.Personal)
| >returns the path to My Documents.
| >
| >Ken
| >-----------------
| >| >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.
|
 
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

| On Tue, 2 Aug 2005 02:40:42 -0400, "Ken Tucker [MVP]"
|
| >Hi,
| >
| > Environment.GetFolderPath(Environment.SpecialFolder.Personal)
| >returns the path to My Documents.
| >
| >Ken
| >-----------------
| >| >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.
|
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top