public NTShare CreateShare(string name, string description, WinAPI.NETAPI32.SHARE_TYPE type, string path) { var newShare = new WinAPI.NETAPI32.SHARE_INFO_502 { shi502_netname = name, shi502_remark = description, shi502_type = (uint)type, shi502_path = path }; uint paramErr; var result = WinAPI.NETAPI32.NetShareAdd(this.Host, 502, ref newShare, out paramErr); if (result != 0) { throw new NetApiException( result, "Unable to create local share '{0}' on host '{1}'", name, Host ); } var share = new NTShare { Host = Host, Name = name }; share.Refresh(); return(share); }
public NTShare[] GetShares(out Exception[] errors) { errors = new Exception[0]; var resumeHandle = 0; var bufPtr = IntPtr.Zero; var localShares = new List <NTShare>(); var errorList = new List <Exception>(); try { int entriesRead; int totalEntries; var result = WinAPI.NETAPI32.NetShareEnum( this.NTCompatibleHostName, 0, ref bufPtr, WinAPI.NETAPI32.MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries, ref resumeHandle ); if (result != 0) { throw new NetApiException( result, "Failed to enumerate shares on host '{0}'", Host ); } var iter = bufPtr; var structSize = Marshal.SizeOf(typeof(WinAPI.NETAPI32.SHARE_INFO_0)); var startAddr = bufPtr.ToInt64(); var endAddr = startAddr + entriesRead * structSize; for (var offset = startAddr; offset < endAddr; offset += structSize) { try { var shareInfo = (WinAPI.NETAPI32.SHARE_INFO_0)Marshal.PtrToStructure( new IntPtr(offset), typeof(WinAPI.NETAPI32.SHARE_INFO_0) ); var share = new NTShare { Host = Host, Name = shareInfo.shi0_netname }; share.Refresh(); localShares.Add(share); } catch (Exception error) { errorList.Add(error); } } } finally { if (bufPtr != IntPtr.Zero) { WinAPI.NETAPI32.NetApiBufferFree(bufPtr); } } errors = errorList.ToArray(); return(localShares.ToArray()); }