Question about reading internet shortcuts

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a Winforms program. I would like to be able to browse the
Favorites subdirectories, select a Internet Shortcut and read its contents.
It does not appear that the standard OpenFileDialog browser will do what I
want. (At least unless the system is currently connected to the internet.
Otherwise it seems to hang.)

Is there a standard Dot Net method for doing this?
 
Example on how to use it from within a form. I have adjusted it so you will
get to see the favorites folders and the shortcuts under the favorites. It
should be refactored though....

private void button1_Click(object sender, EventArgs e)
{
FolderBrowser folderBrowser = new FolderBrowser();
string directoryPath;
DialogResult dialogResult = folderBrowser.ShowDialog(this,
FolderBrowserFolder.Favorites, "Browser for folder", out directoryPath);

if (dialogResult == DialogResult.OK)
{
MessageBox.Show(directoryPath);
}
}

============================================================================
=====

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;

namespace FolderBrowser
{
public enum FolderBrowserFolder
{
Desktop = 0,
Favorites = 6,
MyComputer = 0x11,
MyDocuments = 5,
MyPictures = 0x27,
NetAndDialUpConnections = 0x31,
NetworkNeighborhood = 0x12,
Printers = 4,
Recent = 8,
SendTo = 9,
StartMenu = 11,
Templates = 0x15
}

[Flags]
public enum FolderBrowserStyles
{
// Fields
BrowseForComputer = 0x1000,
BrowseForEverything = 0x4000,
BrowseForPrinter = 0x2000,
RestrictToDomain = 2,
RestrictToFilesystem = 1,
RestrictToSubfolders = 8,
ShowTextBox = 0x10
}

public class FolderBrowser : Component
{
private static readonly int MAX_PATH = 260;

public FolderBrowser()
{
}

public DialogResult ShowDialog(IWin32Window owner, FolderBrowserFolder
startFolder, string title, out string directoryPath)
{
directoryPath = string.Empty;
IntPtr ptr2;
IntPtr ptr1 = IntPtr.Zero;
if (owner != null)
{
ptr2 = owner.Handle;
}
else
{
ptr2 = UnsafeNativeMethods.GetActiveWindow();
}
UnsafeNativeMethods.SHGetSpecialFolderLocation(ptr2, (int)
startFolder, ref ptr1);
if (ptr1 == IntPtr.Zero)
{
return DialogResult.Cancel;
}
int num1 = (int) (FolderBrowserStyles.BrowseForEverything |
((FolderBrowserStyles) ((int)
UnsafeNativeMethods.BrowseInfos.NewDialogStyle)));
if ((num1 & 0x40) != 0)
{
Application.OleRequired();
}
IntPtr ptr3 = IntPtr.Zero;
try
{
UnsafeNativeMethods.BROWSEINFO browseinfo1 = new
UnsafeNativeMethods.BROWSEINFO();
IntPtr ptr4 = Marshal.AllocHGlobal(FolderBrowser.MAX_PATH);
browseinfo1.pidlRoot = ptr1;
browseinfo1.hwndOwner = ptr2;
browseinfo1.pszDisplayName = ptr4;
browseinfo1.lpszTitle = title;
browseinfo1.ulFlags = num1;
browseinfo1.lpfn = IntPtr.Zero;
browseinfo1.lParam = IntPtr.Zero;
browseinfo1.iImage = 0;
ptr3 = UnsafeNativeMethods.SHBrowseForFolder(browseinfo1);
if (ptr3 == IntPtr.Zero)
{
return DialogResult.Cancel;
}
UnsafeNativeMethods.SHGetPathFromIDList(ptr3, ptr4);
directoryPath = Marshal.PtrToStringAuto(ptr4);
Marshal.FreeHGlobal(ptr4);
}
finally
{
UnsafeNativeMethods.IMalloc malloc1 =
FolderBrowser.GetSHMalloc();
malloc1.Free(ptr1);
if (ptr3 != IntPtr.Zero)
{
malloc1.Free(ptr3);
}
}
return DialogResult.OK;
}

private static UnsafeNativeMethods.IMalloc GetSHMalloc()
{
UnsafeNativeMethods.IMalloc[] mallocArray1 = new
UnsafeNativeMethods.IMalloc[1];
UnsafeNativeMethods.SHGetMalloc(mallocArray1);
return mallocArray1[0];
}
}

[SuppressUnmanagedCodeSecurity]
[ComVisible(false)]
internal class UnsafeNativeMethods
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
[ComVisible(false)]
public class BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("00000002-0000-0000-c000-000000000046")]
public interface IMalloc
{
IntPtr Alloc(int cb);
void Free(IntPtr pv);
IntPtr Realloc(IntPtr pv, int cb);
int GetSize(IntPtr pv);
int DidAlloc(IntPtr pv);
void HeapMinimize();
}

[ComVisible(false)]
[Flags]
public enum BrowseInfos
{
// Fields
AllowUrls = 0x80,
BrowseForComputer = 0x1000,
BrowseForEverything = 0x4000,
BrowseForPrinter = 0x2000,
DontGoBelowDomain = 2,
EditBox = 0x10,
NewDialogStyle = 0x40,
ReturnFSAncestors = 8,
ReturnOnlyFSDirs = 1,
ShowShares = 0x8000,
StatusText = 4,
UseNewUI = 80,
Validate = 0x20
}

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr GetActiveWindow();

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);

[DllImport("shell32.dll")]
public static extern int SHGetMalloc([Out]
[MarshalAs(UnmanagedType.LPArray)] IMalloc[] ppMalloc);

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr
pszPath);

[DllImport("shell32.dll")]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int
csidl, ref IntPtr ppidl);
}
}

============================================================================
=======================================

Gabriel Lozano-Morán
 
Excuse me if I am misreading your code (I am not a C# expert) but it looks to
me like this only returns directory names. Also I am not at all clear yet
why you are doing all this coding for what, in VB, would be a simple
FormBrowserDialog call.

Gabriel Lozano-Morán said:
Example on how to use it from within a form. I have adjusted it so you will
get to see the favorites folders and the shortcuts under the favorites. It
should be refactored though....

private void button1_Click(object sender, EventArgs e)
{
FolderBrowser folderBrowser = new FolderBrowser();
string directoryPath;
DialogResult dialogResult = folderBrowser.ShowDialog(this,
FolderBrowserFolder.Favorites, "Browser for folder", out directoryPath);

if (dialogResult == DialogResult.OK)
{
MessageBox.Show(directoryPath);
}
}

============================================================================
=====

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Forms;

namespace FolderBrowser
{
public enum FolderBrowserFolder
{
Desktop = 0,
Favorites = 6,
MyComputer = 0x11,
MyDocuments = 5,
MyPictures = 0x27,
NetAndDialUpConnections = 0x31,
NetworkNeighborhood = 0x12,
Printers = 4,
Recent = 8,
SendTo = 9,
StartMenu = 11,
Templates = 0x15
}

[Flags]
public enum FolderBrowserStyles
{
// Fields
BrowseForComputer = 0x1000,
BrowseForEverything = 0x4000,
BrowseForPrinter = 0x2000,
RestrictToDomain = 2,
RestrictToFilesystem = 1,
RestrictToSubfolders = 8,
ShowTextBox = 0x10
}

public class FolderBrowser : Component
{
private static readonly int MAX_PATH = 260;

public FolderBrowser()
{
}

public DialogResult ShowDialog(IWin32Window owner, FolderBrowserFolder
startFolder, string title, out string directoryPath)
{
directoryPath = string.Empty;
IntPtr ptr2;
IntPtr ptr1 = IntPtr.Zero;
if (owner != null)
{
ptr2 = owner.Handle;
}
else
{
ptr2 = UnsafeNativeMethods.GetActiveWindow();
}
UnsafeNativeMethods.SHGetSpecialFolderLocation(ptr2, (int)
startFolder, ref ptr1);
if (ptr1 == IntPtr.Zero)
{
return DialogResult.Cancel;
}
int num1 = (int) (FolderBrowserStyles.BrowseForEverything |
((FolderBrowserStyles) ((int)
UnsafeNativeMethods.BrowseInfos.NewDialogStyle)));
if ((num1 & 0x40) != 0)
{
Application.OleRequired();
}
IntPtr ptr3 = IntPtr.Zero;
try
{
UnsafeNativeMethods.BROWSEINFO browseinfo1 = new
UnsafeNativeMethods.BROWSEINFO();
IntPtr ptr4 = Marshal.AllocHGlobal(FolderBrowser.MAX_PATH);
browseinfo1.pidlRoot = ptr1;
browseinfo1.hwndOwner = ptr2;
browseinfo1.pszDisplayName = ptr4;
browseinfo1.lpszTitle = title;
browseinfo1.ulFlags = num1;
browseinfo1.lpfn = IntPtr.Zero;
browseinfo1.lParam = IntPtr.Zero;
browseinfo1.iImage = 0;
ptr3 = UnsafeNativeMethods.SHBrowseForFolder(browseinfo1);
if (ptr3 == IntPtr.Zero)
{
return DialogResult.Cancel;
}
UnsafeNativeMethods.SHGetPathFromIDList(ptr3, ptr4);
directoryPath = Marshal.PtrToStringAuto(ptr4);
Marshal.FreeHGlobal(ptr4);
}
finally
{
UnsafeNativeMethods.IMalloc malloc1 =
FolderBrowser.GetSHMalloc();
malloc1.Free(ptr1);
if (ptr3 != IntPtr.Zero)
{
malloc1.Free(ptr3);
}
}
return DialogResult.OK;
}

private static UnsafeNativeMethods.IMalloc GetSHMalloc()
{
UnsafeNativeMethods.IMalloc[] mallocArray1 = new
UnsafeNativeMethods.IMalloc[1];
UnsafeNativeMethods.SHGetMalloc(mallocArray1);
return mallocArray1[0];
}
}

[SuppressUnmanagedCodeSecurity]
[ComVisible(false)]
internal class UnsafeNativeMethods
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
[ComVisible(false)]
public class BROWSEINFO
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}

[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("00000002-0000-0000-c000-000000000046")]
public interface IMalloc
{
IntPtr Alloc(int cb);
void Free(IntPtr pv);
IntPtr Realloc(IntPtr pv, int cb);
int GetSize(IntPtr pv);
int DidAlloc(IntPtr pv);
void HeapMinimize();
}

[ComVisible(false)]
[Flags]
public enum BrowseInfos
{
// Fields
AllowUrls = 0x80,
BrowseForComputer = 0x1000,
BrowseForEverything = 0x4000,
BrowseForPrinter = 0x2000,
DontGoBelowDomain = 2,
EditBox = 0x10,
NewDialogStyle = 0x40,
ReturnFSAncestors = 8,
ReturnOnlyFSDirs = 1,
ShowShares = 0x8000,
StatusText = 4,
UseNewUI = 80,
Validate = 0x20
}

[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr GetActiveWindow();

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder([In] BROWSEINFO lpbi);

[DllImport("shell32.dll")]
public static extern int SHGetMalloc([Out]
[MarshalAs(UnmanagedType.LPArray)] IMalloc[] ppMalloc);

[DllImport("shell32.dll", CharSet=CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr
pszPath);

[DllImport("shell32.dll")]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int
csidl, ref IntPtr ppidl);
}
}

============================================================================
=======================================

Gabriel Lozano-Morán
 
Excuse me if I am misreading your code (I am not a C# expert) but it looks
to
me like this only returns directory names.

No, you will get to see all the folders in your favorites including the
shortcuts, when you select one you will get the complete path to the
internet shortcut
Also I am not at all clear yet why you are doing all this coding for what

Maximum control, I give you the code you can use it as you please. Create
your own FolderBrowserDialog. You can even use this to browse printers, add
printers, ...

Gabriel Lozano-Morán
 
Back
Top