public static void InitializeShare() { SHARE_TYPE type; DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { string shareName = d.Name.Replace(":\\", ""); string shareDesc = ""; string path = d.Name; SHARE_INFO_502 info = new SHARE_INFO_502(); //struttura che descrive la risorsa da condividere info.shi502_netname = shareName; info.shi502_type = SHARE_TYPE.STYPE_DISKTREE | SHARE_TYPE.STYPE_TEMPORARY; //tipo di risorsa da condividere info.shi502_remark = shareDesc; //descrizione opzionale info.shi502_permissions = 0; info.shi502_max_uses = -1; //-1 indica numero di connessioni illimitato info.shi502_current_uses = 0; //numero delle connessioni correnti alla risorsa info.shi502_path = path; //path locale della risorsa da condividere info.shi502_passwd = null; info.shi502_reserved = 0; info.shi502_security_descriptor = IntPtr.Zero; uint error = 0; if ((result = NetShareAdd(null, 502, ref info, out error)) != 0) { Console.WriteLine("result = " + result + " error = " + error); } } }
public void InitializeShare() { SHARE_TYPE type; DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { string shareName = d.Name.Replace(":\\", ""); string shareDesc = ""; string path = d.Name; SHARE_INFO_502 info = new SHARE_INFO_502(); info.shi502_netname = shareName; info.shi502_type = SHARE_TYPE.STYPE_DISKTREE | SHARE_TYPE.STYPE_TEMPORARY; info.shi502_remark = shareDesc; info.shi502_permissions = 0; info.shi502_max_uses = -1; info.shi502_current_uses = 0; info.shi502_path = path; info.shi502_passwd = null; info.shi502_reserved = 0; info.shi502_security_descriptor = IntPtr.Zero; uint error = 0; uint result; if ((result = NetShareAdd(null, 502, ref info, out error)) != 0) { Console.WriteLine("result = " + result + " error = " + error); } } }
// ***** Functions ***** public static SHARE_INFO_502 NetShareGetInfo_502(string ServerName, string ShareName) { Int32 level = 502; IntPtr lShareInfo = IntPtr.Zero; SHARE_INFO_502 shi502_Info = new SHARE_INFO_502(); Int32 result = unmanaged.NetShareGetInfo(ServerName, ShareName, level, ref lShareInfo); if ((Share_ReturnValue)result == Share_ReturnValue.NERR_Success) { shi502_Info = (SHARE_INFO_502)Marshal.PtrToStructure(lShareInfo, typeof(SHARE_INFO_502)); } else { throw new Exception("Unable to get 502 structure. Function returned: " + (Share_ReturnValue)result); } return(shi502_Info); }
public static CreateShareResultType CreateShare(string server, string localPathOnServer, string shareName, string shareDescription) { SHARE_INFO_502 info = new SHARE_INFO_502(); info.shi502_netname = shareName; info.shi502_type = SHARE_TYPE.STYPE_DISKTREE; info.shi502_remark = shareDescription; info.shi502_permissions = 0; info.shi502_max_uses = -1; info.shi502_current_uses = 0; info.shi502_path = localPathOnServer; info.shi502_passwd = null; info.shi502_reserved = 0; info.shi502_security_descriptor = IntPtr.Zero; uint error = 0; uint result = NetShareAdd(server, 502, ref info, out error); return (CreateShareResultType)result; }
public static CreateShareResultType CreateShare(string server, string localPathOnServer, string shareName, string shareDescription) { SHARE_INFO_502 info = new SHARE_INFO_502(); info.shi502_netname = shareName; info.shi502_type = SHARE_TYPE.STYPE_DISKTREE; info.shi502_remark = shareDescription; info.shi502_permissions = 0; info.shi502_max_uses = -1; info.shi502_current_uses = 0; info.shi502_path = localPathOnServer; info.shi502_passwd = null; info.shi502_reserved = 0; info.shi502_security_descriptor = IntPtr.Zero; uint error = 0; uint result = NetShareAdd(server, 502, ref info, out error); return((CreateShareResultType)result); }
protected override string GetTestFilePath(int?index = null, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0) { string testDirectoryPath = Path.GetFullPath(TestDirectory); string shareName = new DirectoryInfo(testDirectoryPath).Name; string fileName = GetTestFileName(index, memberName, lineNumber); SHARE_INFO_502 shareInfo = default; shareInfo.shi502_netname = shareName; shareInfo.shi502_path = testDirectoryPath; shareInfo.shi502_remark = "folder created to test UNC file paths"; shareInfo.shi502_max_uses = -1; int infoSize = Marshal.SizeOf(shareInfo); IntPtr infoBuffer = Marshal.AllocCoTaskMem(infoSize); try { Marshal.StructureToPtr(shareInfo, infoBuffer, false); int shareResult = NetShareAdd(string.Empty, 502, infoBuffer, IntPtr.Zero); if (shareResult != 0 && shareResult != 2118) // is a failure that is not a NERR_DuplicateShare { throw new Exception($"Failed to create a file share, NetShareAdd returned {shareResult}"); } } finally { Marshal.FreeCoTaskMem(infoBuffer); } // now once the folder has been shared we can use "localhost" to access it: // both type of slashes are valid, so let's test one for Debug and another for other configs #if DEBUG return(@$ "//localhost/{shareName}/{fileName}"); #else return(@$ "\\localhost\{shareName}\{fileName}");
private static extern uint NetShareAdd( [MarshalAs(UnmanagedType.LPWStr)] string strServer, Int32 dwLevel, ref SHARE_INFO_502 buf, out uint parm_err );
static void Main(string[] args) { IntPtr bufptr = IntPtr.Zero; int err = NetShareGetInfo("ServerName", "ShareName", 502, out bufptr); if (0 == err) { SHARE_INFO_502 shareInfo = (SHARE_INFO_502)Marshal.PtrToStructure(bufptr, typeof(SHARE_INFO_502)); bool bDaclPresent; bool bDaclDefaulted; IntPtr pAcl = IntPtr.Zero; GetSecurityDescriptorDacl(shareInfo.shi502_security_descriptor, out bDaclPresent, ref pAcl, out bDaclDefaulted); if (bDaclPresent) { ACL_SIZE_INFORMATION AclSize = new ACL_SIZE_INFORMATION(); GetAclInformation(pAcl, ref AclSize, (uint)Marshal.SizeOf(typeof(ACL_SIZE_INFORMATION)), ACL_INFORMATION_CLASS.AclSizeInformation); for (int i = 0; i < AclSize.AceCount; i++) { IntPtr pAce; err = GetAce(pAcl, i, out pAce); ACCESS_ALLOWED_ACE ace = (ACCESS_ALLOWED_ACE)Marshal.PtrToStructure(pAce, typeof(ACCESS_ALLOWED_ACE)); IntPtr iter = (IntPtr)((long)pAce + (long)Marshal.OffsetOf(typeof(ACCESS_ALLOWED_ACE), "SidStart")); byte[] bSID = null; int size = (int)GetLengthSid(iter); bSID = new byte[size]; Marshal.Copy(iter, bSID, 0, size); IntPtr ptrSid; ConvertSidToStringSid(bSID, out ptrSid); string strSID = Marshal.PtrToStringAuto(ptrSid); Console.WriteLine("The details of ACE number {0} are: ", i + 1); StringBuilder name = new StringBuilder(); uint cchName = (uint)name.Capacity; StringBuilder referencedDomainName = new StringBuilder(); uint cchReferencedDomainName = (uint)referencedDomainName.Capacity; SID_NAME_USE sidUse; LookupAccountSid(null, bSID, name, ref cchName, referencedDomainName, ref cchReferencedDomainName, out sidUse); Console.WriteLine("Trustee Name: " + name); Console.WriteLine("Domain Name: " + referencedDomainName); if ((ace.Mask & 0x1F01FF) == 0x1F01FF) { Console.WriteLine("Permission: Full Control"); } else if ((ace.Mask & 0x1301BF) == 0x1301BF) { Console.WriteLine("Permission: READ and CHANGE"); } else if ((ace.Mask & 0x1200A9) == 0x1200A9) { Console.WriteLine("Permission: READ only"); } Console.WriteLine("SID: {0} \nHeader AceType: {1} \nAccess Mask: {2} \nHeader AceFlag: {3}", strSID, ace.Header.AceType.ToString(), ace.Mask.ToString(), ace.Header.AceFlags.ToString()); Console.WriteLine("\n"); } } err = NetApiBufferFree(bufptr); } }
internal static extern NET_API_STATUS NetShareAdd( [MarshalAsAttribute(UnmanagedType.LPWStr)] string servername, uint level, ref SHARE_INFO_502 buf, ref int parm_err);
public static extern NET_API_STATUS NetShareSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string netname, int level, ref SHARE_INFO_502 buf, out uint parm_err);
public static extern NET_API_STATUS NetShareAdd([MarshalAs(UnmanagedType.LPWStr)] string strServer, Int32 dwLevel, ref SHARE_INFO_502 buf, out uint parm_err);
public static uint Test(string path, string shareName, string shareDesc) { //string shareName = "testshare"; //string shareDesc = "This is a test share kekelar2000"; //string path = @"C:\MyShareDirectory"; // do not append comma, it'll fail SHARE_INFO_502 info = new SHARE_INFO_502(); info.shi502_netname = shareName; info.shi502_type = SHARE_TYPE.STYPE_DISKTREE; info.shi502_remark = shareDesc; info.shi502_permissions = 0; // ignored for user-level security info.shi502_max_uses = -1; info.shi502_current_uses = 0; // ignored for set info.shi502_path = path; info.shi502_passwd = null; // ignored for user-level security info.shi502_reserved = 0; info.shi502_security_descriptor = IntPtr.Zero; uint error = 0; return NetShareAdd(null, 502, ref info, out error); }