FolderBrowserDialog
確實允許我瀏覽網絡上的計算機,但它顯示其他不必要的文件夾(我不希望本地文件夾)。另外,我不想選擇文件夾 - 只是計算機名稱。需要一個對話框來瀏覽網絡上的計算機
回答
找到了答案:
的ComputerBrowserDialog
http://discoveringdotnet.alexeyev.org/2008/04/how-to-browse-for-computer-name.html
我tweeked這一點表現得更像的FolderBrowserDialog(只拍了幾張分鐘) 。按照我想要的方式工作。
不過濾
的的FolderBrowserDialog有過濾不支持。例如,它 不可能僅顯示網絡文件夾或僅顯示共享文件夾 或僅顯示以字符串「文檔」開頭的文件夾或具有 特定擴展名的文件。
嘗試使用openFileDialog並設置您的過濾器。
簡單:
private void button1_Click(object sender, EventArgs e)
{
var folderName = GetNetworkFolders(new FolderBrowserDialog());
}
private string GetNetworkFolders(FolderBrowserDialog oFolderBrowserDialog)
{
Type type = oFolderBrowserDialog.GetType();
FieldInfo fieldInfo = type.GetField("rootFolder", BindingFlags.NonPublic | BindingFlags.Instance);
fieldInfo.SetValue(oFolderBrowserDialog, 18);
if (oFolderBrowserDialog.ShowDialog() == DialogResult.OK)
{
return oFolderBrowserDialog.SelectedPath.ToString();
}
else
{
return "";
}
}
這個例子不包括所請求的功能「_Also,我不想要選擇一個文件夾 - 只是計算機名稱。」 – rlemon
除了Rlemon說的,當將18投射到特殊文件夾類型時,該代碼會引發錯誤。說它不存在(出於某種原因它不在.net 3.5中)。 –
它與4.0一起工作,奇怪的是網絡文件夾類型在3.5中不存在 –
ComputerBrowser.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
public class ComputerBrowser
{
private FolderBrowserFolder _startLocation = FolderBrowserFolder.NetworkNeighborhood;
private BrowseInfos _options = BrowseInfos.BrowseForComputer;
private static readonly int MAX_PATH;
private string _title;
private string _displayName;
private string _path;
static ComputerBrowser()
{
MAX_PATH = 260;
}
public bool ShowDialog()
{
return ShowDialog(null);
}
public bool ShowDialog(IWin32Window owner)
{
_path = string.Empty;
IntPtr handle;
IntPtr zero = IntPtr.Zero;
if (owner != null)
handle = owner.Handle;
else
handle = UnmanagedMethods.GetActiveWindow();
UnmanagedMethods.SHGetSpecialFolderLocation(handle, (int)_startLocation, ref zero);
if (zero == IntPtr.Zero)
return false;
int num = (int)_options;
if ((num & 0x40) != 0)
Application.OleRequired();
IntPtr pidl = IntPtr.Zero;
try
{
BrowseInfo lpbi = new BrowseInfo();
//IntPtr pszPath = Marshal.AllocHGlobal(MAX_PATH);
lpbi.pidlRoot = zero;
lpbi.hwndOwner = handle;
lpbi.displayName = new string('\0', MAX_PATH);
lpbi.title = _title;
lpbi.flags = num;
lpbi.callback = null;
lpbi.lparam = IntPtr.Zero;
pidl = UnmanagedMethods.SHBrowseForFolder(ref lpbi);
if (pidl == IntPtr.Zero)
return false;
_displayName = lpbi.displayName;
StringBuilder pathReturned = new StringBuilder(MAX_PATH);
UnmanagedMethods.SHGetPathFromIDList(pidl, pathReturned);
_path = pathReturned.ToString();
UnmanagedMethods.SHMemFree(pidl);
}
finally
{
UnmanagedMethods.SHMemFree(zero);
}
return true;
}
protected 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 BrowseInfos
{
AllowUrls = 0x80,
BrowseForComputer = 0x1000,
BrowseForEverything = 0x4000,
BrowseForPrinter = 0x2000,
DontGoBelowDomain = 2,
ShowTextBox = 0x10,
NewDialogStyle = 0x40,
RestrictToSubfolders = 8,
RestrictToFilesystem = 1,
ShowShares = 0x8000,
StatusText = 4,
UseNewUI = 80,
Validate = 0x20
}
public static string GetComputerName(string title)
{
ComputerBrowser browser = new ComputerBrowser();
browser._title = title;
if (browser.ShowDialog())
return browser._displayName;
else
return string.Empty;
}
}
Unmanaged.cs:
using System;
using System.Runtime.InteropServices;
namespace ActivityMonitor.Monitor.Utils
{
internal delegate int BrowseCallBackProc(IntPtr hwnd, int msg, IntPtr lp, IntPtr wp);
[StructLayout(LayoutKind.Sequential)]
internal struct BrowseInfo
{
public IntPtr hwndOwner;
public IntPtr pidlRoot;
[MarshalAs(UnmanagedType.LPTStr)]
public string displayName;
[MarshalAs(UnmanagedType.LPTStr)]
public string title;
public int flags;
[MarshalAs(UnmanagedType.FunctionPtr)]
public BrowseCallBackProc callback;
public IntPtr lparam;
}
[ComImport]
[Guid("00000002-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
internal interface IMalloc
{
[PreserveSig]
IntPtr Alloc(IntPtr cb);
[PreserveSig]
IntPtr Realloc(IntPtr pv, IntPtr cb);
[PreserveSig]
void Free(IntPtr pv);
[PreserveSig]
IntPtr GetSize(IntPtr pv);
[PreserveSig]
int DidAlloc(IntPtr pv);
[PreserveSig]
void HeapMinimize();
}
/// <summary>
/// A class that defines all the unmanaged methods used in the assembly
/// </summary>
internal class UnmanagedMethods
{
[DllImport("Shell32.dll", CharSet = CharSet.Auto)]
internal extern static System.IntPtr SHBrowseForFolder(ref BrowseInfo bi);
[DllImport("Shell32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool SHGetPathFromIDList(IntPtr pidl, [MarshalAs(UnmanagedType.LPTStr)] System.Text.StringBuilder pszPath);
[DllImport("User32.Dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool SendMessage(IntPtr hwnd, int msg, IntPtr wp, IntPtr lp);
[DllImport("Shell32.dll")]
internal extern static int SHGetMalloc([MarshalAs(UnmanagedType.IUnknown)]out object shmalloc);
[DllImport("user32.dll")]
internal extern static IntPtr GetActiveWindow();
[DllImport("shell32.dll")]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);
//Helper routine to free memory allocated using shells malloc object
internal static void SHMemFree(IntPtr ptr)
{
object shmalloc = null;
if (SHGetMalloc(out shmalloc) == 0)
{
IMalloc malloc = (IMalloc)shmalloc;
(malloc).Free(ptr);
}
}
}
}
- 1. 網絡瀏覽器是否需要在客戶機上安裝IE瀏覽器
- 2. 如何從網絡上的另一臺計算機瀏覽我的Tomcat localhost?
- 3. 對話框的位置不在網絡瀏覽器的中心
- 4. 從同一網絡上的電話打開計算機上的本地網站
- 5. 窗口,設置計算機(瀏覽器)默認網絡代理
- 6. 網絡上的計算機名稱,my.computer.name
- 7. 網絡上的計算機名VB.Net
- 8. Inno Setup如何在瀏覽對話框中顯示網絡?
- 9. C# - 識別另一個網絡上的計算機
- 10. 需要一個公式來計算卷
- 11. 無法從網絡上的其他計算機瀏覽碼頭控制的網絡應用程序
- 12. 用javascript在網絡上ping計算機
- 13. 在網絡上ping計算機
- 14. 計算innerwidth一個jQuery對話框
- 15. 計算機網絡 - BGP
- 16. 我的網絡上的用戶如何瀏覽我的計算機上的特定命名網站?
- 17. 一個PHP的網絡瀏覽器
- 18. 在網絡上的另一臺計算機上啓動進程
- 19. smbtree需要多長時間才能在網絡上找到計算機?
- 20. 從同一網絡上的另一臺計算機Ping Raspberry Pi
- 21. 幫助Windows服務/計劃任務必須使用一個網絡瀏覽器和文件對話框
- 22. 瀏覽jQuery對話框內
- 23. 從網絡瀏覽器中找出計算機的全部可用RAM
- 24. 計算從一個網址到另一個網址瀏覽已爬網網站所需的步驟數量
- 25. Powershell對話框來選擇ping計算機列表
- 26. 需要打開一個圖像在網絡瀏覽器中打開
- 27. 如何從遠程計算機訪問本地計算機上的瀏覽器
- 28. 與不在同一網絡上的計算機進行通信
- 29. 通過PHP獲取網絡上一臺計算機的「狀態」
- 30. SSH到同一網絡上的計算機
的打開文件對話框要求你選擇一個文件。我只想選擇電腦/服務器名稱。 –