读书人

如果共享文件夹并指定用户和权限?解决

发布时间: 2012-03-31 13:13:26 作者: rapoo

如果共享文件夹并指定用户和权限?
比如有个文件夹C:\test

如何使用C#编程实现如下内容:
1)将"C:\test"设置成共享?
2)指定用户DOMAIN1\user为C:\test的用户
3)设—OMAIN1\user的权限

说明:
第1)个问题,我使用NetShareAdd来实现的,可以使用。但是仅仅能将某个文件夹设置成共享,不知道如何才能为它指定用户和权限。

谢谢大家,有线索就散分!

[解决办法]
不知道如何才能为它指定用户和权限。
-----------
局域网内设为同一工作域
[解决办法]
为它指定用户和权限,你可以从属性里设置。
[解决办法]
用程序如何实现,还真不清楚,帮顶。
[解决办法]
up
[解决办法]
UP,我也想知道
[解决办法]
http://www.codeproject.com/KB/IP/StorerActiveDirectory.aspx
[解决办法]
class NetworkHelper
{
/// <summary>
/// 将局域网资源映射给应用程序
/// </summary>
/// <param name="LocalDrive">dbf文件所在的局域网路径</param>
/// <param name="NetworkFolderPath">当行情在局域网机器上使用.比如\\192.168.0.1\dbf</param>
/// <param name="User">访问局域网资源的用户名</param>
/// <param name="Password">访问局域网资源的密码</param>
/// <param name="Force">默认true</param>
/// <returns>true成功,false失败</returns>
public static bool WNetAddConnection(string LocalDrive, string NetworkFolderPath, string User, string Password, bool Force)
{
//2009.9.2 直接返回true,不需要局域网资源映射,by liwei
//return true;

try
{
NetResource netresource = new NetResource();

netresource.Scope = RESOURCE_GLOBALNET;

netresource.Type = RESOURCETYPE_DISK;

netresource.Usage = RESOURCEUSAGE_CONNECTABLE;

netresource.DisplayType = RESOURCEDISPLAYTYPE_SHARE;

netresource.LocalName = LocalDrive;

netresource.RemoteName = NetworkFolderPath;

netresource.Comment = "";

netresource.Provider = "";

int Flag = CONNECT_UPDATE_PROFILE;

if (Force)
{
try
{
WNetCancelConnection(LocalDrive, true);
}
catch (Exception ex)
{
throw ex;
}
}

int result = WNetAddConnection2A(ref netresource, Password, User, Flag);

if (result > 0)
{
throw new System.ComponentModel.Win32Exception(result);
}

return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 解除相应的映射
/// </summary>
/// <param name="LocalDrive">dbf文件所在的局域网路径</param>
/// <param name="Force">默认true</param>
/// <returns>true成功,false失败</returns>
public static bool WNetCancelConnection(string LocalDrive, bool Force)
{
try
{
int result = WNetCancelConnection2(LocalDrive, CONNECT_UPDATE_PROFILE, Force);

if (result > 0)


{
throw new System.ComponentModel.Win32Exception(result);
}

return true;
}
catch (Exception ex)
{
throw ex;
}
}

#region "Constants"

//NetResource Scope
private const int RESOURCE_CONNECTED = 0x00000001;
private const int RESOURCE_GLOBALNET = 0x00000002;
private const int RESOURCE_REMEMBERED = 0x00000003;

//NetResource Type
private const int RESOURCETYPE_ANY = 0x00000000;
private const int RESOURCETYPE_DISK = 0x00000001;
private const int RESOURCETYPE_PRINT = 0x00000002;

//NetResource Usage
private const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;
private const int RESOURCEUSAGE_CONTAINER = 0x00000002;


//NetResource Display Type
private const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;
private const int RESOURCEDISPLAYTYPE_DOMAIN = 0x00000001;
private const int RESOURCEDISPLAYTYPE_SERVER = 0x00000002;
private const int RESOURCEDISPLAYTYPE_SHARE = 0x00000003;
private const int RESOURCEDISPLAYTYPE_FILE = 0x00000004;
private const int RESOURCEDISPLAYTYPE_GROUP = 0x00000005;

//Flags
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_UPDATE_RECENT = 0x00000002;
private const int CONNECT_TEMPORARY = 0x00000004;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_NEED_DRIVE = 0x00000020;

#endregion

#region "Structures"

/// <summary>
/// NetResource contains information about a network resource
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct NetResource
{
public int Scope;
public int Type;
public int DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}

#endregion

#region "Win32 functions"

[DllImport("mpr.dll", EntryPoint = "WNetAddConnection2A", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int WNetAddConnection2A(ref NetResource netresource, string password, string username, int flags);

[DllImport("mpr.dll", EntryPoint = "WNetCancelConnection2", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern int WNetCancelConnection2(string drivename, int flag, bool force);

#endregion
}

读书人网 >C#

热点推荐