本文介绍了Windows7网络中的共享文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1> Class1:-
----------
字符串strServer = @"SERVER";
字符串strShareFolder = @"C:\ MyFolder";
字符串strShareName = @"MF_Test";
字符串strShareDesc = @共享存储";
Share.ADSI_Net.NetApi32.NetError nRetVal = 0;
AD_ShareUtil shUtil =新的AD_ShareUtil();
nRetVal = shUtil.CreateShare(strServer,strShareFolder,strShareName,strShareDesc,false);
如果(nRetVal == Share.ADSI_Net.NetApi32.NetError.NERR_Success)
{Console.WriteLine("Share {0} created",strShareName);}
否则(nRetVal == ADSI_Net.NetApi32.NetError.NERR_DuplicateShare)
{Console.WriteLine(共享{0}已存在",strShareName);}
其他
Console.WriteLine(未创建共享",strShareName);

字符串目录= @"C:\ MyFolder";
DirectorySecurity sec = Directory.GetAccessControl(directory);

NTAccount acct =新的NTAccount(每个人");
IdentityReference id = acct.Translate(typeof(NTAccount));
FileSystemAccessRule规则=新FileSystemAccessRule(acct,FileSystemRights.FullControl,AccessControlType.Allow);
sec.AddAccessRule(rule);
Directory.SetAccessControl(directory,sec);

Console.ReadLine();

2> Class2:-
--------
命名空间Share
{
类ADSI_Net
{
公共类NetApi32
{
公共枚举NetError
{
error code(错误代码)= 0,
NERR_BASE = 2100,
NERR_UnknownDevDir =(NERR_BASE + 16),
NERR_DuplicateShare =(NERR_BASE + 18),
NERR_BufTooSmall =(NERR_BASE + 23),
}

公共枚举SHARE_TYPE:ulong
{
STYPE_DISKTREE = 0,
STYPE_PRINTQ = 1,
STYPE_DEVICE = 2,
STYPE_IPC = 3,
STYPE_SPECIAL = 0x80000000,
}

[StructLayout(LayoutKind.Sequential)]
公共结构SHARE_INFO_502
{
[MarshalAs(UnmanagedType.LPWStr)]
公用字符串shi502_netname;
公共uint shi502_type;
[MarshalAs(UnmanagedType.LPWStr)]
公共字符串shi502_remark;
公共Int32 shi502_permissions;
公共Int32 shi502_max_uses;
公共Int32 shi502_current_uses;
[MarshalAs(UnmanagedType.LPWStr)]
公共字符串shi502_path;
公共IntPtr shi502_passwd;
公共Int32 shi502_reserved;
公共IntPtr shi502_security_descriptor;
}

[DllImport("Netapi32.dll")]
公共静态外部int NetShareAdd([MarshalAs(UnmanagedType.LPWStr)]
字符串strServer,Int32 dwLevel,IntPtr buf,IntPtr parm_err);
}
}

类AD_ShareUtil
{
公共Share.ADSI_Net.NetApi32.NetError CreateShare(字符串strServer,字符串strPath,字符串strShareName,字符串strShareDesc,布尔bAdmin)
{
Share.ADSI_Net.NetApi32.SHARE_INFO_502 shInfo =新的ADSI_Net.NetApi32.SHARE_INFO_502();
shInfo.shi502_netname = strShareName;
shInfo.shi502_type =(uint)Share.ADSI_Net.NetApi32.SHARE_TYPE.STYPE_DISKTREE;
如果(bAdmin)
{
shInfo.shi502_type = uint)Share.ADSI_Net.NetApi32.SHARE_TYPE.STYPE_SPECIAL;
shInfo.shi502_netname + ="$";
}
shInfo.shi502_permissions = 0;
shInfo.shi502_path = strPath;
shInfo.shi502_passwd = IntPtr.Zero;
shInfo.shi502_remark = strShareDesc;
shInfo.shi502_max_uses = -1;
shInfo.shi502_security_descriptor = IntPtr.Zero;

字符串strTargetServer = strServer;
如果(strServer.Length!= 0)
{
strTargetServer = strServer;
如果(strServer [0]!=``\\'')
{strTargetServer ="\\\\" + strServer;}
}
int nRetValue = 0;
//调用Net API添加共享..
int nStSize = Marshal.SizeOf(shInfo);
IntPtr缓冲区= Marshal.AllocCoTaskMem(nStSize);
Marshal.StructureToPtr(shInfo,buffer,false);
nRetValue = Share.ADSI_Net.NetApi32.NetShareAdd(strTargetServer,502,buffer,IntPtr.Zero);
Marshal.FreeCoTaskMem(buffer);
返回(Share.ADSI_Net.NetApi32.NetError)nRetValue;
}
}
}

我的上述两节课是在Windows XP SP2 Professional中实现网络共享的良好工作.
但无法在Windows7中实现网络共享.

1>Class1 :-
----------
string strServer = @"SERVER";
string strShareFolder = @"C:\MyFolder";
string strShareName = @"MF_Test";
string strShareDesc = @"Share to store";
Share.ADSI_Net.NetApi32.NetError nRetVal = 0;
AD_ShareUtil shUtil = new AD_ShareUtil();
nRetVal = shUtil.CreateShare(strServer, strShareFolder, strShareName, strShareDesc, false);
if (nRetVal == Share.ADSI_Net.NetApi32.NetError.NERR_Success)
{Console.WriteLine("Share {0} created", strShareName);}
else if (nRetVal == ADSI_Net.NetApi32.NetError.NERR_DuplicateShare)
{Console.WriteLine("Share {0} already exists", strShareName);}
else
Console.WriteLine("Share not created", strShareName);

String directory = @"C:\MyFolder";
DirectorySecurity sec = Directory.GetAccessControl(directory);

NTAccount acct = new NTAccount("Everyone");
IdentityReference id = acct.Translate(typeof(NTAccount));
FileSystemAccessRule rule = new FileSystemAccessRule(acct, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(rule);
Directory.SetAccessControl(directory, sec);

Console.ReadLine();

2>Class2 :-
--------
namespace Share
{
class ADSI_Net
{
public class NetApi32
{
public enum NetError
{
NERR_Success = 0,
NERR_BASE = 2100,
NERR_UnknownDevDir = (NERR_BASE + 16),
NERR_DuplicateShare = (NERR_BASE + 18),
NERR_BufTooSmall = (NERR_BASE + 23),
}

public enum SHARE_TYPE : ulong
{
STYPE_DISKTREE = 0,
STYPE_PRINTQ = 1,
STYPE_DEVICE = 2,
STYPE_IPC = 3,
STYPE_SPECIAL = 0x80000000,
}

[StructLayout(LayoutKind.Sequential)]
public struct SHARE_INFO_502
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_netname;
public uint shi502_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_remark;
public Int32 shi502_permissions;
public Int32 shi502_max_uses;
public Int32 shi502_current_uses;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi502_path;
public IntPtr shi502_passwd;
public Int32 shi502_reserved;
public IntPtr shi502_security_descriptor;
}

[DllImport("Netapi32.dll")]
public static extern int NetShareAdd([MarshalAs(UnmanagedType.LPWStr)]
string strServer, Int32 dwLevel, IntPtr buf, IntPtr parm_err);
}
}

class AD_ShareUtil
{
public Share.ADSI_Net.NetApi32.NetError CreateShare(string strServer, string strPath, string strShareName, string strShareDesc, bool bAdmin)
{
Share.ADSI_Net.NetApi32.SHARE_INFO_502 shInfo = new ADSI_Net.NetApi32.SHARE_INFO_502();
shInfo.shi502_netname = strShareName;
shInfo.shi502_type = (uint)Share.ADSI_Net.NetApi32.SHARE_TYPE.STYPE_DISKTREE;
if (bAdmin)
{
shInfo.shi502_type = uint)Share.ADSI_Net.NetApi32.SHARE_TYPE.STYPE_SPECIAL;
shInfo.shi502_netname += "$";
}
shInfo.shi502_permissions = 0;
shInfo.shi502_path = strPath;
shInfo.shi502_passwd = IntPtr.Zero;
shInfo.shi502_remark = strShareDesc;
shInfo.shi502_max_uses = -1;
shInfo.shi502_security_descriptor = IntPtr.Zero;

string strTargetServer = strServer;
if (strServer.Length != 0)
{
strTargetServer = strServer;
if (strServer[0] != ''\\'')
{strTargetServer = "\\\\" + strServer;}
}
int nRetValue = 0;
// Call Net API to add the share..
int nStSize = Marshal.SizeOf(shInfo);
IntPtr buffer = Marshal.AllocCoTaskMem(nStSize);
Marshal.StructureToPtr(shInfo, buffer, false);
nRetValue = Share.ADSI_Net.NetApi32.NetShareAdd(strTargetServer, 502, buffer, IntPtr.Zero);
Marshal.FreeCoTaskMem(buffer);
return (Share.ADSI_Net.NetApi32.NetError)nRetValue;
}
}
}

My above two class is fine work to network share in Windows XP SP2 Professional.
But not working to network share in Windows7.

any help me that to create network share folder in any OS.

推荐答案



这篇关于Windows7网络中的共享文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 06:59