1)建立datagrid資料列/模板列/按鈕的操作類:

來源:互聯網
上載者:User
datagrid|按鈕|建立|模板|資料  

//寫這個程式,為了是顯示區域網路內的主機,然後通過使用者點擊選中的機器,檢查是否該主機上安裝有SQLSERVER,是為一個配置程式所寫的
//這裡提供CODE,給有需要的朋友

//設定DLL必須使用該命名空間
using System.Runtime.InteropServices;

  #region 動態連結程式庫定義
  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetEnumResource(IntPtr hEnum, ref int lpcCount,IntPtr lpBuffer, ref int lpBufferSize );
  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetOpenEnum(RESOURCE_SCOPE dwScope,RESOURCE_TYPE dwType,RESOURCE_USAGE dwUsage,[MarshalAs(UnmanagedType.AsAny)][In] Object lpNetResource,out IntPtr lphEnum);
  [DllImport("mpr.dll",CharSet=CharSet.Auto)] private static extern int WNetCloseEnum( IntPtr hEnum );
  #endregion

  #region 定義枚舉常量
  public enum RESOURCE_SCOPE
  {
   RESOURCE_CONNECTED = 0x00000001,
   RESOURCE_GLOBALNET = 0x00000002,
   RESOURCE_REMEMBERED = 0x00000003,
   RESOURCE_RECENT= 0x00000004,
   RESOURCE_CONTEXT= 0x00000005
  }

  public enum RESOURCE_TYPE
  {
   RESOURCETYPE_ANY= 0x00000000,
   RESOURCETYPE_DISK= 0x00000001,
   RESOURCETYPE_PRINT = 0x00000002,
   RESOURCETYPE_RESERVED = 0x00000008,
  }

  public enum RESOURCE_USAGE
  {
   RESOURCEUSAGE_CONNECTABLE =0x00000001,
   RESOURCEUSAGE_CONTAINER=0x00000002,
   RESOURCEUSAGE_NOLOCALDEVICE =0x00000004,
   RESOURCEUSAGE_SIBLING=0x00000008,
   RESOURCEUSAGE_ATTACHED=0x00000010,
   RESOURCEUSAGE_ALL =(RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED),
  }

  public enum RESOURCE_DISPLAYTYPE
  {
   RESOURCEDISPLAYTYPE_GENERIC= 0x00000000,
   RESOURCEDISPLAYTYPE_DOMAIN= 0x00000001,
   RESOURCEDISPLAYTYPE_SERVER= 0x00000002,

   RESOURCEDISPLAYTYPE_SHARE= 0x00000003,
   RESOURCEDISPLAYTYPE_FILE = 0x00000004,
   RESOURCEDISPLAYTYPE_GROUP= 0x00000005,
   RESOURCEDISPLAYTYPE_NETWORK= 0x00000006,
   RESOURCEDISPLAYTYPE_ROOT = 0x00000007,
   RESOURCEDISPLAYTYPE_SHAREADMIN = 0x00000008,
   RESOURCEDISPLAYTYPE_DIRECTORY = 0x00000009,
   RESOURCEDISPLAYTYPE_TREE = 0x0000000A,
   RESOURCEDISPLAYTYPE_NDSCONTAINER = 0x0000000B
  }

  public struct NETRESOURCE
  {
   public RESOURCE_SCOPE dwScope;
   public RESOURCE_TYPE dwType;
   public RESOURCE_DISPLAYTYPE dwDisplayType;
   public RESOURCE_USAGE dwUsage;
   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpLocalName;
   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpRemoteName;
   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpComment;
   [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPTStr)] public string lpProvider;
  }

  public enum NERR
  {
   NERR_Success = 0,/* Success */
   ERROR_MORE_DATA = 234, // dderror
   ERROR_NO_BROWSER_SERVERS_FOUND = 6118,
   ERROR_INVALID_LEVEL = 124,
   ERROR_ACCESS_DENIED = 5,
   ERROR_INVALID_PARAMETER = 87,
   ERROR_NOT_ENOUGH_MEMORY = 8,
   ERROR_NETWORK_BUSY = 54,
   ERROR_BAD_NETPATH = 53,
   ERROR_NO_NETWORK = 1222,
   ERROR_INVALID_HANDLE_STATE = 1609,
   ERROR_EXTENDED_ERROR= 1208
  }
  #endregion


  #region 開始列舉網路函數
  public TreeNode pNode1;
  public TreeNode pNode2;
  public TreeNode pNode3;
  private void WNETOE(Object o)
  {
   int iRet;
   IntPtr ptrHandle = new IntPtr();
   try
   {
    iRet =WNetOpenEnum(RESOURCE_SCOPE.RESOURCE_GLOBALNET,RESOURCE_TYPE.RESOURCETYPE_ANY,RESOURCE_USAGE.RESOURCEUSAGE_ALL,o,out ptrHandle );
    if( iRet != 0 )return;

    int entries;
    int buffer = 16384;
    IntPtr ptrBuffer = Marshal.AllocHGlobal(buffer);
    NETRESOURCE nr;
    
    for(;;)
    {
     entries = -1;
     buffer = 16384;
     iRet =WNetEnumResource(ptrHandle,ref entries,ptrBuffer,ref buffer);
     if((iRet != 0) || (entries<1))break;

     Int32 ptr = ptrBuffer.ToInt32();
     for(int i=0;i<entries;i++ )
     {
      nr = (NETRESOURCE)Marshal.PtrToStructure( new IntPtr(ptr), typeof(NETRESOURCE) );
      if(RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER == (nr.dwUsage & RESOURCE_USAGE.RESOURCEUSAGE_CONTAINER))
      {
       
       ptr += Marshal.SizeOf( nr );
       if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_NETWORK)
       {
        pNode1 = tView.Nodes.Add(nr.lpRemoteName);//TreeView
        pNode1.ImageIndex = 0;
        pNode1.SelectedImageIndex = 0;
       }
       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_DOMAIN)
       {
        pNode2 = pNode1.Nodes.Add(nr.lpRemoteName);
        pNode2.ImageIndex = 1;
        pNode2.SelectedImageIndex =1;
       }
       else if(nr.dwDisplayType == RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SERVER)
       {
        string sPCName = nr.lpRemoteName;
        pNode3 = pNode2.Nodes.Add(sPCName.Substring(2,sPCName.Length-2));\\此處為去處機器名稱前的兩個"\\"字元
        pNode3.ImageIndex = 2;
        pNode3.SelectedImageIndex = 2;
       }

       WNETOE(nr);
      }
      
     }
    }
    Marshal.FreeHGlobal( ptrBuffer );
    iRet =WNetCloseEnum( ptrHandle );
   }
   catch(Exception ex)
   {
    MessageBox.Show("尋找本網錯誤:"+ex.Message,"錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error);
   }
  }

  #endregion

  //表單LOAD事件中,使用函數
  private void frmDBConnConfig_Load(object sender, System.EventArgs e)
  {
   WNETOE(null);
  }



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。