主题:通过.net实现2台服务器共享文件
//共享类型
private const long STYPE_ALL = -1;
private const long STYPE_DISKTREE = 0;
private const long STYPE_PRINTQ = 1;
private const long STYPE_DEVICE = 2;
private const long STYPE_IPC = 3;
private const long STYPE_SPECIAL = 0X80000000;
//权限
private const long ACCESS_READ = 0X1;
private const long ACCESS_WRITE = 0X2;
private const long ACCESS_CREATE = 0X4;
private const long ACCESS_EXEC = 0X8;
private const long ACCESS_DELETE = 0X10;
private const long ACCESS_ATRIB = 0X20;
private const long ACCESS_PERM = 0X40;
private const long ACCESS_ALL = ACCESS_READ | ACCESS_WRITE | ACCESS_CREATE | ACCESS_EXEC | ACCESS_DELETE | ACCESS_ATRIB | ACCESS_PERM;
//出错信息
private const int ERROR_ACCESS_DENIED = 5;
private const int ERROR_INVALID_LEVEL = 124;
private const int ERROR_INVALID_NAME = 123;// dderror
private const int DNS_ERROR_INVALID_NAME = ERROR_INVALID_NAME;
private const int ERROR_INVALID_PARAMETER = 87;// dderror
private const int NERR_BASE = 2100;
private const int NERR_DuplicateShare = (NERR_BASE+18); /* The name has already been shared. */
//共享信息
private struct SHARE_INFO_2
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_netname ; //共享名
public long shi2_type ; //类型
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_remark ; //备注
public long shi2_permissions ; //权限
public long shi2_max_uses ; //最大用户
public string shi2_current_uses ; //
public string shi2_path ; //路径
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_pswd ; //密码
}
//设置共享
[System.Runtime.InteropServices.DllImport("netapi32")]
private static extern int NetShareAdd(string ServerName,int level,ref SHARE_INFO_2 buf,long parmerr);
//删除共享
[System.Runtime.InteropServices.DllImport("netapi32")]
private static extern int NetShareDel(string ServerName, [MarshalAs(UnmanagedType.LPWStr)]ref string ShareName, int dword);
【本文转载自:[url=http://www.beidaqingniao.org]http://www.beidaqingniao.org[/url]】
private const long STYPE_ALL = -1;
private const long STYPE_DISKTREE = 0;
private const long STYPE_PRINTQ = 1;
private const long STYPE_DEVICE = 2;
private const long STYPE_IPC = 3;
private const long STYPE_SPECIAL = 0X80000000;
//权限
private const long ACCESS_READ = 0X1;
private const long ACCESS_WRITE = 0X2;
private const long ACCESS_CREATE = 0X4;
private const long ACCESS_EXEC = 0X8;
private const long ACCESS_DELETE = 0X10;
private const long ACCESS_ATRIB = 0X20;
private const long ACCESS_PERM = 0X40;
private const long ACCESS_ALL = ACCESS_READ | ACCESS_WRITE | ACCESS_CREATE | ACCESS_EXEC | ACCESS_DELETE | ACCESS_ATRIB | ACCESS_PERM;
//出错信息
private const int ERROR_ACCESS_DENIED = 5;
private const int ERROR_INVALID_LEVEL = 124;
private const int ERROR_INVALID_NAME = 123;// dderror
private const int DNS_ERROR_INVALID_NAME = ERROR_INVALID_NAME;
private const int ERROR_INVALID_PARAMETER = 87;// dderror
private const int NERR_BASE = 2100;
private const int NERR_DuplicateShare = (NERR_BASE+18); /* The name has already been shared. */
//共享信息
private struct SHARE_INFO_2
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_netname ; //共享名
public long shi2_type ; //类型
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_remark ; //备注
public long shi2_permissions ; //权限
public long shi2_max_uses ; //最大用户
public string shi2_current_uses ; //
public string shi2_path ; //路径
[MarshalAs(UnmanagedType.LPWStr)]
public string shi2_pswd ; //密码
}
//设置共享
[System.Runtime.InteropServices.DllImport("netapi32")]
private static extern int NetShareAdd(string ServerName,int level,ref SHARE_INFO_2 buf,long parmerr);
//删除共享
[System.Runtime.InteropServices.DllImport("netapi32")]
private static extern int NetShareDel(string ServerName, [MarshalAs(UnmanagedType.LPWStr)]ref string ShareName, int dword);
【本文转载自:[url=http://www.beidaqingniao.org]http://www.beidaqingniao.org[/url]】