Si vas a copiar

.. y pegar este post en tu web o blog personal, por favor te pido que coloques el link del post de donde lo copiastes:

Fuente:

BrowserFolder en C# y VB.NET

16 de agosto de 2010 No hay comentarios.:
Necesitado en un BrowserFolder me encontre con este:

Codigo C#:

folderBrowserDialog1.Description = "Buscar el directorio";

// Para tener un directorio de inicio propio debes primero
// asignar a RootFolder = Environment.SpecialFolder.MyComputer
// luego en la siguiente linea escribes el directorio de inicio
// en SelectedPath

folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
folderBrowserDialog1.SelectedPath = Application.StartupPath; // <-- Directorio de inicio

if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtReportsPath.Text = folderBrowserDialog1.SelectedPath;
}


Codigo VB.NET:

folderBrowserDialog1.Description = "Buscar el directorio"
' Para tener un directorio de inicio propio debes primero
' asignar a RootFolder = Environment.SpecialFolder.MyComputer
' luego en la siguiente linea escribes el directorio de inicio
' en SelectedPath
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer
folderBrowserDialog1.SelectedPath = Application.StartupPath ' <-- Directorio de inicio
If folderBrowserDialog1.ShowDialog() = DialogResult.OK Then
txtReportsPath.Text = folderBrowserDialog1.SelectedPath
End if


Pues bien o mejor dicho mal, ya que este control no permite ver carpetas en la red, si que malo no???

Buscando en la web, me encontre con la funcion SHBrowseForFolder de la API de Windows, segui buscando y encontre este codigo que tambien venia sin el InitDir, asi que lo modifique:

Codigo C#:

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WINAPI32
{
/*
* Codigo original de netomatix.com
* http://www.netomatix.com/FolderBrowser.aspx
*
* Modificado para tener un directorio de inicio por: tttony 2010
* http://tttony.blogspot.com/
*
* POR FAVOR NO BORRAR ESTE COMENTARIO
*/
public delegate int BrowseCallbackProc(IntPtr hwnd, int uMsg, IntPtr wParam, IntPtr lParam);

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

}

public class Win32SDK
{
[DllImport("shell32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);

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

[DllImport("shell32.dll", PreserveSig = true, CharSet = CharSet.Auto)]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, string lParam);

}

public enum BrowseForFolderMessages
{
BFFM_ENABLEOK = 0x465,
BFFM_INITIALIZED = 1,
BFFM_IUNKNOWN = 5,
BFFM_SELCHANGED = 2,
BFFM_SETEXPANDED = 0x46a,
BFFM_SETOKTEXT = 0x469,
BFFM_SETSELECTIONA = 0x466,
BFFM_SETSELECTIONW = 0x467,
BFFM_SETSTATUSTEXTA = 0x464,
BFFM_SETSTATUSTEXTW = 0x468,
BFFM_VALIDATEFAILEDA = 3,
BFFM_VALIDATEFAILEDW = 4
}

[Flags, Serializable]
public enum BrowseFlags
{
BIF_DEFAULT = 0x0000,
BIF_BROWSEFORCOMPUTER = 0x1000,
BIF_BROWSEFORPRINTER = 0x2000,
BIF_BROWSEINCLUDEFILES = 0x4000,
BIF_BROWSEINCLUDEURLS = 0x0080,
BIF_DONTGOBELOWDOMAIN = 0x0002,
BIF_EDITBOX = 0x0010,
BIF_NEWDIALOGSTYLE = 0x0040,
BIF_NONEWFOLDERBUTTON = 0x0200,
BIF_RETURNFSANCESTORS = 0x0008,
BIF_RETURNONLYFSDIRS = 0x0001,
BIF_SHAREABLE = 0x8000,
BIF_STATUSTEXT = 0x0004,
BIF_UAHINT = 0x0100,
BIF_VALIDATE = 0x0020,
BIF_NOTRANSLATETARGETS = 0x0400,
}

public class FolderBrowser
{
private string m_strDirectoryPath;
private string m_strTitle;
private string m_strDisplayName;
private BrowseFlags m_Flags;
private string m_initDir = null;

public FolderBrowser()
{
m_Flags = BrowseFlags.BIF_DEFAULT;
m_strTitle = "";
}

public string DirectoryPath
{
get { return this.m_strDirectoryPath; }
}

public string DisplayName
{
get { return this.m_strDisplayName; }
}

public string Title
{
set { this.m_strTitle = value; }
}

public BrowseFlags Flags
{
set { this.m_Flags = value; }
}

public DialogResult ShowDialog()
{
BROWSEINFO bi = new BROWSEINFO();
bi.pszDisplayName = IntPtr.Zero;
//bi.lpfn = IntPtr.Zero;
/*
* InitDir??
*/
bi.lpfn = new BrowseCallbackProc(browserCallBack);
bi.lParam = IntPtr.Zero;
bi.lpszTitle = "Select Folder";
IntPtr idListPtr = IntPtr.Zero;
IntPtr pszPath = IntPtr.Zero;
try
{
if (this.m_strTitle.Length != 0)
{
bi.lpszTitle = this.m_strTitle;
}
bi.ulFlags = (int)this.m_Flags;
bi.pszDisplayName = Marshal.AllocHGlobal(256);
// Call SHBrowseForFolder
idListPtr = Win32SDK.SHBrowseForFolder(bi);

// Check if the user cancelled out of the dialog or not.
if (idListPtr == IntPtr.Zero)
{
return DialogResult.Cancel;
}

// Allocate ncessary memory buffer to receive the folder path.
pszPath = Marshal.AllocHGlobal(256);
// Call SHGetPathFromIDList to get folder path.
bool bRet = Win32SDK.SHGetPathFromIDList(idListPtr, pszPath);
// Convert the returned native poiner to string.
m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath);
this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
return DialogResult.Abort;
}
finally
{
// Free the memory allocated by shell.
if (idListPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(idListPtr);
}
if (pszPath != IntPtr.Zero)
{
Marshal.FreeHGlobal(pszPath);
}
if (bi != null)
{
Marshal.FreeHGlobal(bi.pszDisplayName);
}
}
return DialogResult.OK;
}

private IntPtr GetStartLocationPath()
{
return IntPtr.Zero;
}

public string InitDir
{
set { this.m_initDir = value; }
}

private int browserCallBack(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam)
{
if (uMsg == (int)BrowseForFolderMessages.BFFM_INITIALIZED)
{
/*
* Con BFFM_SETSELECTIONW todo bien
*/
Win32SDK.SendMessage(hWnd, (int)BrowseForFolderMessages.BFFM_SETSELECTIONW, 1, this.m_initDir);
}

return 0;
}
}

}


Codigo VB.NET:

Imports System.Diagnostics
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms
Imports System.Runtime.InteropServices

Namespace WINAPI32
'
' * Codigo original de netomatix.com
' * http://www.netomatix.com/FolderBrowser.aspx
' *
' * Modificado para tener un directorio de inicio por: tttony 2010
' * http://tttony.blogspot.com/
' *
' * POR FAVOR NO BORRAR ESTE COMENTARIO
'

Public Delegate Function BrowseCallbackProc(hwnd As IntPtr, uMsg As Integer, wParam As IntPtr, lParam As IntPtr) As Integer

_
_
Public Class BROWSEINFO
Public hwndOwner As IntPtr
Public pidlRoot As IntPtr
Public pszDisplayName As IntPtr
Public lpszTitle As String
Public ulFlags As Integer
Public lpfn As BrowseCallbackProc
Public lParam As IntPtr
Public iImage As Integer

End Class

Public Class Win32SDK
_
Public Shared Function SHBrowseForFolder(bi As BROWSEINFO) As IntPtr
End Function

_
Public Shared Function SHGetPathFromIDList(pidl As IntPtr, pszPath As IntPtr) As Boolean
End Function

_
Public Shared Function SHGetSpecialFolderLocation(hwnd As IntPtr, csidl As Integer, ByRef ppidl As IntPtr) As Integer
End Function

_
Public Shared Function SendMessage(hWnd As IntPtr, Msg As Integer, wParam As Integer, lParam As String) As IntPtr
End Function

End Class

Public Enum BrowseForFolderMessages
BFFM_ENABLEOK = &H465
BFFM_INITIALIZED = 1
BFFM_IUNKNOWN = 5
BFFM_SELCHANGED = 2
BFFM_SETEXPANDED = &H46a
BFFM_SETOKTEXT = &H469
BFFM_SETSELECTIONA = &H466
BFFM_SETSELECTIONW = &H467
BFFM_SETSTATUSTEXTA = &H464
BFFM_SETSTATUSTEXTW = &H468
BFFM_VALIDATEFAILEDA = 3
BFFM_VALIDATEFAILEDW = 4
End Enum

_
Public Enum BrowseFlags
BIF_DEFAULT = &H0
BIF_BROWSEFORCOMPUTER = &H1000
BIF_BROWSEFORPRINTER = &H2000
BIF_BROWSEINCLUDEFILES = &H4000
BIF_BROWSEINCLUDEURLS = &H80
BIF_DONTGOBELOWDOMAIN = &H2
BIF_EDITBOX = &H10
BIF_NEWDIALOGSTYLE = &H40
BIF_NONEWFOLDERBUTTON = &H200
BIF_RETURNFSANCESTORS = &H8
BIF_RETURNONLYFSDIRS = &H1
BIF_SHAREABLE = &H8000
BIF_STATUSTEXT = &H4
BIF_UAHINT = &H100
BIF_VALIDATE = &H20
BIF_NOTRANSLATETARGETS = &H400
End Enum

Public Class FolderBrowser
Private m_strDirectoryPath As String
Private m_strTitle As String
Private m_strDisplayName As String
Private m_Flags As BrowseFlags
Private m_initDir As String = Nothing

Public Sub New()
m_Flags = BrowseFlags.BIF_DEFAULT
m_strTitle = ""
End Sub

Public ReadOnly Property DirectoryPath() As String
Get
Return Me.m_strDirectoryPath
End Get
End Property

Public ReadOnly Property DisplayName() As String
Get
Return Me.m_strDisplayName
End Get
End Property

Public WriteOnly Property Title() As String
Set
Me.m_strTitle = value
End Set
End Property

Public WriteOnly Property Flags() As BrowseFlags
Set
Me.m_Flags = value
End Set
End Property

Public Function ShowDialog() As DialogResult
Dim bi As New BROWSEINFO()
bi.pszDisplayName = IntPtr.Zero
'bi.lpfn = IntPtr.Zero;
'
' * InitDir??
'

bi.lpfn = New BrowseCallbackProc(AddressOf browserCallBack)
bi.lParam = IntPtr.Zero
bi.lpszTitle = "Select Folder"
Dim idListPtr As IntPtr = IntPtr.Zero
Dim pszPath As IntPtr = IntPtr.Zero
Try
If Me.m_strTitle.Length <> 0 Then
bi.lpszTitle = Me.m_strTitle
End If
bi.ulFlags = CInt(Me.m_Flags)
bi.pszDisplayName = Marshal.AllocHGlobal(256)
' Call SHBrowseForFolder
idListPtr = Win32SDK.SHBrowseForFolder(bi)

' Check if the user cancelled out of the dialog or not.
If idListPtr = IntPtr.Zero Then
Return DialogResult.Cancel
End If

' Allocate ncessary memory buffer to receive the folder path.
pszPath = Marshal.AllocHGlobal(256)
' Call SHGetPathFromIDList to get folder path.
Dim bRet As Boolean = Win32SDK.SHGetPathFromIDList(idListPtr, pszPath)
' Convert the returned native poiner to string.
m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath)
Me.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName)
Catch ex As Exception
Trace.WriteLine(ex.Message)
Return DialogResult.Abort
Finally
' Free the memory allocated by shell.
If idListPtr <> IntPtr.Zero Then
Marshal.FreeHGlobal(idListPtr)
End If
If pszPath <> IntPtr.Zero Then
Marshal.FreeHGlobal(pszPath)
End If
If bi IsNot Nothing Then
Marshal.FreeHGlobal(bi.pszDisplayName)
End If
End Try
Return DialogResult.OK
End Function

Private Function GetStartLocationPath() As IntPtr
Return IntPtr.Zero
End Function

Public WriteOnly Property InitDir() As String
Set
Me.m_initDir = value
End Set
End Property

Private Function browserCallBack(hWnd As IntPtr, uMsg As Integer, wParam As IntPtr, lParam As IntPtr) As Integer
If uMsg = CInt(BrowseForFolderMessages.BFFM_INITIALIZED) Then
'
' * Con BFFM_SETSELECTIONW todo bien
'

Win32SDK.SendMessage(hWnd, CInt(BrowseForFolderMessages.BFFM_SETSELECTIONW), 1, Me.m_initDir)
End If

Return 0
End Function
End Class

End Namespace



Como usarlo??

Codigo C#:

FolderBrowser browser = new FolderBrowser();
browser.Title = "Buscar el directorio";
browser.Flags = BrowseFlags.BIF_NEWDIALOGSTYLE | BrowseFlags.BIF_NONEWFOLDERBUTTON;
browser.InitDir = Application.StartupPath; // <-- el directorio de inicio
if (browser.ShowDialog() == DialogResult.OK)
{
txtPath.Text = browser.DirectoryPath;
}


Codigo VB.NET:

Dim browser As New FolderBrowser()
browser.Title = "Buscar el directorio"
browser.Flags = BrowseFlags.BIF_NEWDIALOGSTYLE Or BrowseFlags.BIF_NONEWFOLDERBUTTON
browser.InitDir = Application.StartupPath ' <-- el directorio de inicio
If browser.ShowDialog() = DialogResult.OK Then
txtPath.Text = browser.DirectoryPath
End if



Publicado en tttony.blogspot.com

Buscar en el Blog



PUBLICIDAD