public static bool SetShareCSC(string server, string shareName, CSCType cscState) { bool ret = false; NativeMethods.SHARE_INFO_1005 share_info_1005 = new NativeMethods.SHARE_INFO_1005(); share_info_1005.shi1005_flags = (uint)cscState; IntPtr parm_err = IntPtr.Zero; IntPtr share_info_1005_ptr = IntPtr.Zero; share_info_1005_ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(share_info_1005)); Marshal.StructureToPtr(share_info_1005, share_info_1005_ptr, false); int retVal = NativeMethods.NetShareSetInfo(server, shareName, 1005, share_info_1005_ptr, out parm_err); if (retVal == 0) ret = true; //free up our non-managed memory NativeMethods.NetApiBufferFree(share_info_1005_ptr); NativeMethods.NetApiBufferFree(parm_err); return ret; }
public static CSCType GetShareCSCState(string server, string shareName) { CSCType curCscType = CSCType.CSC_CACHE_NONE; //check the share IntPtr ptr = IntPtr.Zero; int errCode = NativeMethods.NetShareGetInfo(server, shareName, 1005, out ptr); if ((NativeMethods.NET_API_STATUS)errCode == NativeMethods.NET_API_STATUS.NERR_Success) { //get the share_info_1005 structure NativeMethods.SHARE_INFO_1005 shareInfo = new NativeMethods.SHARE_INFO_1005(); shareInfo = (NativeMethods.SHARE_INFO_1005)Marshal.PtrToStructure(ptr, typeof(NativeMethods.SHARE_INFO_1005)); curCscType = (CSCType)shareInfo.shi1005_flags; //clear out the unmanaged ptr - if errCode = true, Win32's NetShareGetInfo() fills in the ptr with a structure compatible with NativeMethods.SHARE_INFO_502 Marshal.FreeHGlobal(ptr); } return curCscType; }