Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 23-Jan-12 5:02am    
"Not working" is not informative. What happens?
--SA

1 solution

Most likely this is just because of permissions issue. It's not enough to log in as administrator in Windows 7. Try to run it as administrator with elevated privileges. Here is how: http://www.sevenforums.com/tutorials/11841-run-administrator.html[^].

Next time, run it under debugger or catch the exception and provide comprehensive exception dump. Make a code sample and comment the lines where exception is thrown/propagated. "Not working" is not informative.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900