国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

C#中使用網(wǎng)絡(luò)函數(shù) (第一部分 用戶函數(shù))[翻譯]

2024-07-21 02:26:52
字體:
供稿:網(wǎng)友
本文將著重介紹win32 api庫中涉及網(wǎng)絡(luò)管理的函數(shù)。首先我要講一講在.net框架中管理用戶的兩個方法,第一種是active directory方法,這種方法要求你安裝active directory。如果你打算管理一個小網(wǎng)絡(luò)上的用戶,或者一個未安裝active directory的獨立工作站,為了管理用戶而安裝active directory顯得有些不值得。另外一種方法則是這篇文章所要講到的——使用win32 api庫函數(shù)。在這篇文章中,我將介紹如何使用c# 添加、刪除和修改用戶和組,以及如何查詢一個主機或網(wǎng)絡(luò)的用戶和網(wǎng)絡(luò)信息。我們將用到以下函數(shù)
  • netuseradd
  • netuserdel
  • netusergetinfo
  • netusersetinfo
  • netuserchangepassword
  • netuserenum
  • netusergetlocalgroups

初始化

首先,正如許多c#開發(fā)者都知道的,我們要引入interopservices名字空間到我們的工程中,以便能夠訪問dll中的函數(shù)。這可以通過如下的代碼片斷實現(xiàn):

///// 代碼片斷 1.0

using system.runtime.interopservices;

//// 代碼結(jié)束

一旦我們擁有了訪問權(quán)限,我們就可以將dll中的函數(shù)聲明引入,我們將使用方法和結(jié)構(gòu)體來關(guān)聯(lián)它們。函數(shù)調(diào)用將在下面討論:

使用c#添加一個用戶

在網(wǎng)絡(luò)函數(shù)中最重要的操作之一就是向一個網(wǎng)絡(luò)或計算機添加一個用戶。要通過c#添加一個用戶,我們需要使用netadduser函數(shù),該函數(shù)允許我們添加用戶到特定的計算機,如果我們將servername置空,用戶將被添加到本地計算機。在下面的代碼片斷中,我們將看到如何聲明和使用netuseradd函數(shù)。我們在使用該函數(shù)前,需要定義一個結(jié)構(gòu)體user_info_1,來作為netuseradd的參數(shù)。

///// 代碼片斷 1.1 聲明 

 

 

 

 

 

 

[dllimport("netapi32.dll")]
extern static int netuseradd([marshalas(unmanagedtype.lpwstr)] string servername, int level, ref user_info_1 buf, int parm_err);
//// 代碼結(jié)束

在使用代碼時,你要注意到一點——盡管使用了最后一個int參數(shù),你不需要知道返回的錯誤值。如果你一定要了解錯誤,需要修改代碼。既然我們已經(jīng)聲明了我們要使用的外部api,我們應(yīng)該聲明結(jié)構(gòu)體了。
/**////// 代碼片斷 1.2 結(jié)構(gòu)體聲明

[structlayout(layoutkind.sequential, charset
=charset.unicode)]
public struct
 user_info_1
{
public string
 usri1_name; 
public string
 usri1_password; 
public int
 usri1_password_age; 
public int
 usri1_priv; 
public string
 usri1_home_dir; 
public string
 comment; 
public int
 usri1_flags; 
public string
 usri1_script_path;
}
 

/**/
//// 代碼結(jié)束

在聲明之后,我們就可以在我們的程序中調(diào)用該函數(shù)。下面是netuseradd的如何使用的代碼:
/**////// 代碼片斷 1.3 netuseradd

user_info_1 newuser 
= new user_info_1(); // create an new instance of the user_info_1 struct

newuser.usri1_name 
= "usertestone"// allocates the username
newuser.usri1_password = "password"// allocates the password
newuser.usri1_priv = 1// sets the account type to user_priv_user
newuser.usri1_home_dir = null// we didn't supply a home directory
newuser.comment = "my first user made through c#"// comment on the user
newuser.usri1_script_path = null// we didn't supply a logon script path

if(netuseradd(null ,1 ,ref newuser, 0)!=0// if the call fails we get a non-zero value
{
messagebox.show(
"error adding user","error",messageboxbuttons.ok,messageboxicon.error);
}

/**///// 代碼結(jié)束
 
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 保山市| 信宜市| 乡宁县| 石泉县| 绥芬河市| 衡东县| 屏东县| 岳阳县| 莫力| 新安县| 加查县| 玛纳斯县| 武强县| 二连浩特市| 九台市| 都兰县| 潍坊市| 卫辉市| 嵩明县| 呼玛县| 榕江县| 高淳县| 邵阳市| 寻甸| 富阳市| 康平县| 宁化县| 兰考县| 桐梓县| 石门县| 苏尼特右旗| 望江县| 洮南市| 田林县| 兴文县| 遂川县| 东丽区| 当阳市| 清镇市| 涿鹿县| 东阳市|