Ejemplo n.º 1
0
        /// <summary>
        /// Enumerate shares of specific machine. If no machine is specified, local machine is used
        /// </summary>
        /// <returns>Collection of <see cref="QuickIOShareInfo"/></returns>
        public static IEnumerable <QuickIOShareInfo> EnumerateShares <T>(QuickIOShareApiReadLevel level, String machineName = null) where T : IWin32ApiShareInfo
        {
            Contract.Ensures(Contract.Result <IEnumerable <QuickIOShareInfo> >() != null);

            // Specify
            string machine = String.IsNullOrEmpty(machineName) ? Environment.MachineName : machineName;
            IntPtr buffer  = IntPtr.Zero;

            try
            {
                int resumeHandle = 0;
                int entriesRead, totalEntries;
                int returnCode = Win32SafeNativeMethods.NetShareEnum(machineName, ( int )level, out buffer, -1, out entriesRead, out totalEntries, ref resumeHandle);

                // Available return codes: http://msdn.microsoft.com/en-us/library/windows/desktop/bb525387(v=vs.85).aspx

                //if( returnCode == Win32ErrorCodes.ERROR_ACCESS_DENIED ) // Access Denied
                //{
                //    level = QuickIOShareApiReadLevel.Normal
                //    // Admin required, but not granted? try with normal usr
                //    returnCode = InternalQuickIO.GetShareEnumResult( machine, level, ref buffer, ref entriesRead, ref totalEntries, ref resumeHandle );
                //}

                //// skip if zero or no entries
                //if( returnCode != 0 || entriesRead <= 0 ) // we only handle return 0 here
                //{
                //    yield break;
                //}

                if (returnCode > 0)
                {
                    Type type     = typeof(T);
                    int  typeSize = Marshal.SizeOf(type);

                    for (int i = 0, currentDataItem = buffer.ToInt32(); i < entriesRead; i++, currentDataItem += typeSize)
                    {
                        IWin32ApiShareInfo shareInfo = ( T )Marshal.PtrToStructure(new IntPtr(currentDataItem), typeof(T));
                        yield return(new QuickIOShareInfo(machine, shareInfo.GetShareName(), shareInfo.GetShareType(), shareInfo.GetRemark()));
                    }
                }
            }
            finally
            {
                // Clean up buffer allocated by system
                // TODO: check if this is enough here
                if (buffer != IntPtr.Zero)
                {
                    Win32SafeNativeMethods.NetApiBufferFree(buffer);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Enumerate shares of specific machine. If no machine is specified, local machine is used
        /// </summary>
        /// <returns>Collection of <see cref="QuickIOShareInfo"/></returns>
        public static IEnumerable<QuickIOShareInfo> EnumerateShares( String machineName = null, QuickIOShareApiReadLevel level = QuickIOShareApiReadLevel.Admin )
        {
            Contract.Ensures( Contract.Result<IEnumerable<QuickIOShareInfo>>() != null );

            Type shareType = ( level == QuickIOShareApiReadLevel.Admin ) ? typeof( Win32ApiShareInfoAdmin ) : typeof( Win32ApiShareInfoNormal );
            switch( level )
            {
                case QuickIOShareApiReadLevel.Admin:
                    return InternalQuickIO.EnumerateShares<Win32ApiShareInfoAdmin>( QuickIOShareApiReadLevel.Admin, machineName );
                case QuickIOShareApiReadLevel.Normal:
                    return InternalQuickIO.EnumerateShares<Win32ApiShareInfoNormal>( QuickIOShareApiReadLevel.Normal, machineName );
                default:
                    throw new NotSupportedException( $"Unsupported level '{level}'" );
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Enumerate shares of specific machine. If no machine is specified, local machine is used
 /// </summary>
 /// <returns>Collection of <see cref="QuickIOShareInfo"/></returns>
 public static Task<IEnumerable<QuickIOShareInfo>> EnumerateSharesAsync( String machineName = null, QuickIOShareApiReadLevel level = QuickIOShareApiReadLevel.Admin )
 {
     return NETCompatibility.AsyncExtensions.GetAsyncResult( () => EnumerateShares( machineName, level ) );
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Gets Share Result
 /// </summary>
 /// <param name="machineName">Machine</param>
 /// <param name="level">API level</param>
 /// <param name="buffer">Buffer</param>
 /// <param name="entriesRead">Entries total read</param>
 /// <param name="totalEntries">Entries total</param>
 /// <param name="resumeHandle">Handle</param>
 /// <returns>Error Code</returns>
 /// <remarks>http://msdn.microsoft.com/en-us/library/windows/desktop/bb525387(v=vs.85).aspx</remarks>
 public static int GetShareEnumResult( string machineName, QuickIOShareApiReadLevel level, ref IntPtr buffer, ref int entriesRead, ref int totalEntries, ref int resumeHandle )
 {
     return Win32SafeNativeMethods.NetShareEnum( machineName, ( int ) level, out buffer, -1, out entriesRead, out totalEntries, ref  resumeHandle );
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Enumerate shares of specific machine. If no machine is specified, local machine is used
        /// </summary>
        /// <returns>Collection of <see cref="QuickIOShareInfo"/></returns>
        public static IEnumerable <QuickIOShareInfo> EnumerateShares(String machineName = null, QuickIOShareApiReadLevel level = QuickIOShareApiReadLevel.Admin)
        {
            Contract.Ensures(Contract.Result <IEnumerable <QuickIOShareInfo> >() != null);

            Type shareType = (level == QuickIOShareApiReadLevel.Admin) ? typeof(Win32ApiShareInfoAdmin) : typeof(Win32ApiShareInfoNormal);

            switch (level)
            {
            case QuickIOShareApiReadLevel.Admin:
                return(QuickIOEngine.EnumerateShares <Win32ApiShareInfoAdmin>(QuickIOShareApiReadLevel.Admin, machineName));

            case QuickIOShareApiReadLevel.Normal:
                return(QuickIOEngine.EnumerateShares <Win32ApiShareInfoNormal>(QuickIOShareApiReadLevel.Normal, machineName));

            default:
                throw new NotSupportedException($"Unsupported level '{level}'");
            }
        }