用API [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);参考http://www.wangchao.net.cn/bbsdetail_72646.html
cxunxun的专栏 登录注册全站当前博客空间博客好友相册留言 c#获取所有局域网IP收藏 using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Configuration; namespace ETable ...{ /**//// <summary> /// ETable 的摘要说明。 /// </summary> public class ETable : System.Web.UI.Page ...{ protected System.Web.UI.HtmlControls.HtmlInputFile File1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.DataGrid DataGrid1; public Random rd; private void Page_Load(object sender, System.EventArgs e) ...{ // 在此处放置用户代码以初始化页面 } Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) ...{ // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() ...{ this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void Button1_Click(object sender, System.EventArgs e) ...{ if (File1.PostedFile!=null) ...{ rd=new Random(1); string filename=DateTime.Now.Date.ToString("yyyymmdd")+DateTime.Now.ToLongTimeString().Replace(":","")+rd.Next(9999).ToString()+".xls"; File1.PostedFile.SaveAs(@Server.MapPath("file/")+filename); //Response.Write(File1.PostedFile.FileName.ToString()); //Response.Write("上传成功"); Label1.Text="文件名为"+filename; string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+Server.MapPath("file")+"/"+filename+";Extended Properties=Excel 8.0" ; OleDbConnection thisconnection=new OleDbConnection(conn); thisconnection.Open(); string Sql="select * from [Sheet1$]"; OleDbDataAdapter mycommand=new OleDbDataAdapter(Sql,thisconnection); DataSet ds=new DataSet(); mycommand.Fill(ds,"[Sheet1$]"); thisconnection.Close(); DataGrid1.DataSource=ds; DataGrid1.DataBind(); string conn1="User ID=sa;Data Source=127.0.0.1;Password=sa;Initial Catalog=index;Provider=SQLOLEDB.1;"; OleDbConnection thisconnection1=new OleDbConnection(conn1); thisconnection1.Open(); int count=ds.Tables["[Sheet1$]"].Rows.Count; for (int i=0;i<count;i++) ...{ string id,id_1,id_2,id_3; id=ds.Tables["[Sheet1$]"].Rows[i]["id"].ToString(); id_1=ds.Tables["[Sheet1$]"].Rows[i]["id_1"].ToString(); id_2=ds.Tables["[Sheet1$]"].Rows[i]["id_2"].ToString(); id_3=ds.Tables["[Sheet1$]"].Rows[i]["id_3"].ToString(); string excelsql="insert into excel(id,id_1,id_2,id_3) values ('"+id+"','"+id_1+"','"+id_2+"','"+id_3+"') "; OleDbCommand mycommand1=new OleDbCommand(excelsql,thisconnection1); mycommand1.ExecuteNonQuery(); } Response.Write("更新成功"); thisconnection1.Close(); } } } } 发表于 @ 2008年04月03日 16:32:00|评论(loading...)|编辑 | 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cxunxun/archive/2008/04/03/2247723.aspx
CodeProject上面有个现成的: http://www.codeproject.com/KB/IP/ListNetworkComputers.aspxusing System; using System.Collections; using System.Runtime.InteropServices; using System.Security;public sealed class NetworkBrowser { #region Dll Imports //declare the Netapi32 : NetServerEnum method import [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true)] [SuppressUnmanagedCodeSecurityAttribute] /// <summary> /// Netapi32.dll : The NetServerEnum function lists all servers /// of the specified type that are /// visible in a domain. For example, an /// application can call NetServerEnum /// to list all domain controllers only /// or all SQL servers only. /// You can combine bit masks to list /// several types. For example, a value /// of 0x00000003 combines the bit /// masks for SV_TYPE_WORKSTATION /// (0x00000001) and SV_TYPE_SERVER (0x00000002) /// </summary> public static extern int NetServerEnum( string ServerNane, // must be null int dwLevel, ref IntPtr pBuf, int dwPrefMaxLen, out int dwEntriesRead, out int dwTotalEntries, int dwServerType, string domain, // null for login domain out int dwResumeHandle ); //declare the Netapi32 : NetApiBufferFree method import [DllImport("Netapi32", SetLastError = true)] [SuppressUnmanagedCodeSecurity] /// <summary> /// Netapi32.dll : The NetApiBufferFree function frees /// the memory that the NetApiBufferAllocate function allocates. /// Call NetApiBufferFree to free /// the memory that other network /// management functions return. /// </summary> public static extern int NetApiBufferFree( IntPtr pBuf); //create a _SERVER_INFO_100 STRUCTURE [StructLayout(LayoutKind.Sequential)] public struct _SERVER_INFO_100 { internal int sv100_platform_id; [MarshalAs(UnmanagedType.LPWStr)] internal string sv100_name; } #endregion #region Public Constructor /// <SUMMARY> /// Constructor, simply creates a new NetworkBrowser object /// </SUMMARY> public NetworkBrowser() { } #endregion /// <summary> /// Uses the DllImport : NetServerEnum /// with all its required parameters /// (see http://msdn.microsoft.com/library/default.asp? /// url=/library/en-us/netmgmt/netmgmt/netserverenum.asp /// for full details or method signature) to /// retrieve a list of domain SV_TYPE_WORKSTATION /// and SV_TYPE_SERVER PC's /// </summary> /// <returns>Arraylist that represents /// all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER /// PC's in the Domain</returns> public ArrayList getNetworkComputers() { //local fields ArrayList networkComputers = new ArrayList(); const int MAX_PREFERRED_LENGTH = -1; int SV_TYPE_WORKSTATION = 1; int SV_TYPE_SERVER = 2; IntPtr buffer = IntPtr.Zero; IntPtr tmpBuffer = IntPtr.Zero; int entriesRead = 0; int totalEntries = 0; int resHandle = 0; int sizeofINFO = Marshal.SizeOf(typeof (_SERVER_INFO_100)); try { //call the DllImport : NetServerEnum //with all its required parameters //see http://msdn.microsoft.com/library/ //default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp //for full details of method signature int ret = NetServerEnum(null, 100, ref buffer, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, SV_TYPE_WORKSTATION | SV_TYPE_SERVER, null, out resHandle); //if the returned with a NERR_Success //(C++ term), =0 for C# if (ret == 0) { //loop through all SV_TYPE_WORKSTATION //and SV_TYPE_SERVER PC's for (int i = 0; i < totalEntries; i++) { //get pointer to, Pointer to the //buffer that received the data from //the call to NetServerEnum. //Must ensure to use correct size of //STRUCTURE to ensure correct //location in memory is pointed to tmpBuffer = new IntPtr((int) buffer + (i * sizeofINFO)); //Have now got a pointer to the list //of SV_TYPE_WORKSTATION and //SV_TYPE_SERVER PC's, which is unmanaged memory //Needs to Marshal data from an //unmanaged block of memory to a //managed object, again using //STRUCTURE to ensure the correct data //is marshalled _SERVER_INFO_100 svrInfo = (_SERVER_INFO_100) Marshal.PtrToStructure(tmpBuffer, typeof (_SERVER_INFO_100)); //add the PC names to the ArrayList networkComputers.Add(svrInfo.sv100_name); } } } catch (Exception ex) { // MessageBox.Show("Problem with acessing " + // "network computers in NetworkBrowser " + // "\r\n\r\n\r\n" + ex.Message, // "Error", MessageBoxButtons.OK, // MessageBoxIcon.Error); return null; } finally { //The NetApiBufferFree function frees //the memory that the //NetApiBufferAllocate function allocates NetApiBufferFree(buffer); } //return entries found return networkComputers; } }
[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);参考http://www.wangchao.net.cn/bbsdetail_72646.html
登录注册全站当前博客空间博客好友相册留言 c#获取所有局域网IP收藏
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Configuration; namespace ETable
...{
/**//// <summary>
/// ETable 的摘要说明。
/// </summary>
public class ETable : System.Web.UI.Page
...{
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public Random rd; private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
} Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
...{
if (File1.PostedFile!=null)
...{ rd=new Random(1);
string filename=DateTime.Now.Date.ToString("yyyymmdd")+DateTime.Now.ToLongTimeString().Replace(":","")+rd.Next(9999).ToString()+".xls";
File1.PostedFile.SaveAs(@Server.MapPath("file/")+filename);
//Response.Write(File1.PostedFile.FileName.ToString());
//Response.Write("上传成功");
Label1.Text="文件名为"+filename;
string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+Server.MapPath("file")+"/"+filename+";Extended Properties=Excel 8.0" ;
OleDbConnection thisconnection=new OleDbConnection(conn);
thisconnection.Open();
string Sql="select * from [Sheet1$]";
OleDbDataAdapter mycommand=new OleDbDataAdapter(Sql,thisconnection);
DataSet ds=new DataSet();
mycommand.Fill(ds,"[Sheet1$]");
thisconnection.Close();
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
string conn1="User ID=sa;Data Source=127.0.0.1;Password=sa;Initial Catalog=index;Provider=SQLOLEDB.1;";
OleDbConnection thisconnection1=new OleDbConnection(conn1);
thisconnection1.Open();
int count=ds.Tables["[Sheet1$]"].Rows.Count; for (int i=0;i<count;i++)
...{
string id,id_1,id_2,id_3;
id=ds.Tables["[Sheet1$]"].Rows[i]["id"].ToString();
id_1=ds.Tables["[Sheet1$]"].Rows[i]["id_1"].ToString();
id_2=ds.Tables["[Sheet1$]"].Rows[i]["id_2"].ToString();
id_3=ds.Tables["[Sheet1$]"].Rows[i]["id_3"].ToString();
string excelsql="insert into excel(id,id_1,id_2,id_3) values ('"+id+"','"+id_1+"','"+id_2+"','"+id_3+"') ";
OleDbCommand mycommand1=new OleDbCommand(excelsql,thisconnection1);
mycommand1.ExecuteNonQuery();
}
Response.Write("更新成功");
thisconnection1.Close(); }
}
}
}
发表于 @ 2008年04月03日 16:32:00|评论(loading...)|编辑 | 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cxunxun/archive/2008/04/03/2247723.aspx
http://www.codeproject.com/KB/IP/ListNetworkComputers.aspxusing System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security;public sealed class NetworkBrowser {
#region Dll Imports //declare the Netapi32 : NetServerEnum method import
[DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true)]
[SuppressUnmanagedCodeSecurityAttribute]
/// <summary>
/// Netapi32.dll : The NetServerEnum function lists all servers
/// of the specified type that are
/// visible in a domain. For example, an
/// application can call NetServerEnum
/// to list all domain controllers only
/// or all SQL servers only.
/// You can combine bit masks to list
/// several types. For example, a value
/// of 0x00000003 combines the bit
/// masks for SV_TYPE_WORKSTATION
/// (0x00000001) and SV_TYPE_SERVER (0x00000002)
/// </summary>
public static extern int NetServerEnum(
string ServerNane, // must be null
int dwLevel,
ref IntPtr pBuf,
int dwPrefMaxLen,
out int dwEntriesRead,
out int dwTotalEntries,
int dwServerType,
string domain, // null for login domain
out int dwResumeHandle
); //declare the Netapi32 : NetApiBufferFree method import
[DllImport("Netapi32", SetLastError = true)]
[SuppressUnmanagedCodeSecurity]
/// <summary>
/// Netapi32.dll : The NetApiBufferFree function frees
/// the memory that the NetApiBufferAllocate function allocates.
/// Call NetApiBufferFree to free
/// the memory that other network
/// management functions return.
/// </summary>
public static extern int NetApiBufferFree(
IntPtr pBuf); //create a _SERVER_INFO_100 STRUCTURE
[StructLayout(LayoutKind.Sequential)]
public struct _SERVER_INFO_100 {
internal int sv100_platform_id; [MarshalAs(UnmanagedType.LPWStr)]
internal string sv100_name;
} #endregion #region Public Constructor /// <SUMMARY>
/// Constructor, simply creates a new NetworkBrowser object
/// </SUMMARY>
public NetworkBrowser() {
} #endregion /// <summary>
/// Uses the DllImport : NetServerEnum
/// with all its required parameters
/// (see http://msdn.microsoft.com/library/default.asp?
/// url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
/// for full details or method signature) to
/// retrieve a list of domain SV_TYPE_WORKSTATION
/// and SV_TYPE_SERVER PC's
/// </summary>
/// <returns>Arraylist that represents
/// all the SV_TYPE_WORKSTATION and SV_TYPE_SERVER
/// PC's in the Domain</returns>
public ArrayList getNetworkComputers() {
//local fields
ArrayList networkComputers = new ArrayList();
const int MAX_PREFERRED_LENGTH = -1;
int SV_TYPE_WORKSTATION = 1;
int SV_TYPE_SERVER = 2;
IntPtr buffer = IntPtr.Zero;
IntPtr tmpBuffer = IntPtr.Zero;
int entriesRead = 0;
int totalEntries = 0;
int resHandle = 0;
int sizeofINFO = Marshal.SizeOf(typeof (_SERVER_INFO_100)); try {
//call the DllImport : NetServerEnum
//with all its required parameters
//see http://msdn.microsoft.com/library/
//default.asp?url=/library/en-us/netmgmt/netmgmt/netserverenum.asp
//for full details of method signature
int ret = NetServerEnum(null, 100, ref buffer,
MAX_PREFERRED_LENGTH,
out entriesRead,
out totalEntries, SV_TYPE_WORKSTATION |
SV_TYPE_SERVER, null, out
resHandle);
//if the returned with a NERR_Success
//(C++ term), =0 for C#
if (ret == 0) {
//loop through all SV_TYPE_WORKSTATION
//and SV_TYPE_SERVER PC's
for (int i = 0; i < totalEntries; i++) {
//get pointer to, Pointer to the
//buffer that received the data from
//the call to NetServerEnum.
//Must ensure to use correct size of
//STRUCTURE to ensure correct
//location in memory is pointed to
tmpBuffer = new IntPtr((int) buffer +
(i * sizeofINFO));
//Have now got a pointer to the list
//of SV_TYPE_WORKSTATION and
//SV_TYPE_SERVER PC's, which is unmanaged memory
//Needs to Marshal data from an
//unmanaged block of memory to a
//managed object, again using
//STRUCTURE to ensure the correct data
//is marshalled
_SERVER_INFO_100 svrInfo = (_SERVER_INFO_100)
Marshal.PtrToStructure(tmpBuffer,
typeof (_SERVER_INFO_100)); //add the PC names to the ArrayList
networkComputers.Add(svrInfo.sv100_name);
}
}
} catch (Exception ex) {
// MessageBox.Show("Problem with acessing " +
// "network computers in NetworkBrowser " +
// "\r\n\r\n\r\n" + ex.Message,
// "Error", MessageBoxButtons.OK,
// MessageBoxIcon.Error);
return null;
} finally {
//The NetApiBufferFree function frees
//the memory that the
//NetApiBufferAllocate function allocates
NetApiBufferFree(buffer);
}
//return entries found
return networkComputers;
}
}