My Documents path in C#

Y

Yosh

What is the best method to obtain the actual folder path to My Documents for
the current logged in user in C#?

Thanks,

Yosh
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Take a look at Environment class, there you have methods to get it, IIRC
there is an enum with several folders that you use calling GetFolderPath()
method

Cheers,
 
G

Gabriel Lozano-Morán

1)
Environment.GetFolderPath(Environment.SpecialFolder.Personal)

2)
Or the extreme way what is actually done by the GetFolderPath method but
with less checking :p

[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
IntPtr hToken, int dwFlags, System.Text.StringBuilder lpszPath);
[STAThread]
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, 5, IntPtr.Zero, 0, sb);
Console.WriteLine(sb);
Console.Read();
}


Gabriel Lozano-Morán
 
Y

Yosh

Is one method better than the other?

Do they both always return the My Documents path or is there a situation in
which one wouldn't?

Thanks,

Yosh

Gabriel Lozano-Morán said:
1)
Environment.GetFolderPath(Environment.SpecialFolder.Personal)

2)
Or the extreme way what is actually done by the GetFolderPath method but
with less checking :p

[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
IntPtr hToken, int dwFlags, System.Text.StringBuilder lpszPath);
[STAThread]
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, 5, IntPtr.Zero, 0, sb);
Console.WriteLine(sb);
Console.Read();
}


Gabriel Lozano-Morán


Yosh said:
What is the best method to obtain the actual folder path to My Documents
for the current logged in user in C#?

Thanks,

Yosh
 
G

Gabriel Lozano-Morán

I posted the two ways to let you know why I love .NET, you would offcourse
go for number 1 :)

Gabriel Lozano-Morán

Yosh said:
Is one method better than the other?

Do they both always return the My Documents path or is there a situation
in which one wouldn't?

Thanks,

Yosh

Gabriel Lozano-Morán said:
1)
Environment.GetFolderPath(Environment.SpecialFolder.Personal)

2)
Or the extreme way what is actually done by the GetFolderPath method but
with less checking :p

[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
IntPtr hToken, int dwFlags, System.Text.StringBuilder lpszPath);
[STAThread]
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, 5, IntPtr.Zero, 0, sb);
Console.WriteLine(sb);
Console.Read();
}


Gabriel Lozano-Morán


Yosh said:
What is the best method to obtain the actual folder path to My Documents
for the current logged in user in C#?

Thanks,

Yosh
 
Y

Yosh

ah yes... that's exactly what I chose.

Thanks Grabriel!

Yosh

Gabriel Lozano-Morán said:
I posted the two ways to let you know why I love .NET, you would offcourse
go for number 1 :)

Gabriel Lozano-Morán

Yosh said:
Is one method better than the other?

Do they both always return the My Documents path or is there a situation
in which one wouldn't?

Thanks,

Yosh

Gabriel Lozano-Morán said:
1)
Environment.GetFolderPath(Environment.SpecialFolder.Personal)

2)
Or the extreme way what is actually done by the GetFolderPath method but
with less checking :p

[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int
nFolder, IntPtr hToken, int dwFlags, System.Text.StringBuilder
lpszPath);
[STAThread]
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder(260);
SHGetFolderPath(IntPtr.Zero, 5, IntPtr.Zero, 0, sb);
Console.WriteLine(sb);
Console.Read();
}


Gabriel Lozano-Morán


What is the best method to obtain the actual folder path to My
Documents for the current logged in user in C#?

Thanks,

Yosh
 

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

Top