コード例 #1
0
ファイル: Share.cs プロジェクト: joeostrander/LockSmith
        /// <summary>
        /// Returns the local <see cref="Share"/> object with the best match
        /// to the specified path.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Share PathToShare(string fileName)
        {
            if (null == fileName || 0 == fileName.Length)
            {
                return(null);
            }

            fileName = Path.GetFullPath(fileName);

            if (!IsValidFilePath(fileName))
            {
                return(null);
            }

            ShareCollection shi = LocalShares;

            if (null == shi)
            {
                return(null);
            }
            else
            {
                return(shi[fileName]);
            }
        }
コード例 #2
0
ファイル: Share.cs プロジェクト: joeostrander/LockSmith
        /// <summary>
        /// sorcery by Joe Ostrander
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string UncPathToRemoteFilePath(string uncFilePath)
        {
            if (string.IsNullOrEmpty(uncFilePath))
            {
                return(null);
            }
            if (!uncFilePath.StartsWith(@"\\"))
            {
                return(null);
            }
            if (!System.IO.File.Exists(uncFilePath))
            {
                return(null);
            }

            //Get server name from UNC
            string          server     = "";
            string          share      = "";
            string          remainder  = "";
            string          pattern    = @"\\\\(?<server>[^\\]+)\\(?<share>[^\\]+)(?<remainder>\\.*)";
            Regex           re         = new Regex(pattern, RegexOptions.IgnoreCase);
            MatchCollection colMatches = re.Matches(uncFilePath);

            if (colMatches.Count > 0)
            {
                server    = colMatches[0].Groups["server"].Value;
                share     = colMatches[0].Groups["share"].Value;
                remainder = colMatches[0].Groups["remainder"].Value;
            }
            else
            {
                return(null);
            }

            if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(share) || string.IsNullOrEmpty(remainder))
            {
                return(null);
            }


            ShareCollection shi = GetShares(server);

            if (shi == null)
            {
                return(null);
            }
            else
            {
                foreach (Share sh in shi)
                {
                    if (sh.NetName.ToLower() == share.ToLower())
                    {
                        //Console.WriteLine(sh.Path+remainder);
                        return(sh.Path + remainder);
                    }
                }
                return(null);
            }
        }
コード例 #3
0
ファイル: Shares.cs プロジェクト: sillsdev/WorldPad
        /// <summary>
        /// Enumerates the shares on Windows 9x
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateShares9x(string server, ShareCollection shares, ref bool accessDenied)
        {
            int    level = 50;
            int    nRet = 0;
            ushort entriesRead, totalEntries;

            accessDenied = false;
            Type   t        = typeof(SHARE_INFO_50);
            int    size     = Marshal.SizeOf(t);
            ushort cbBuffer = (ushort)(MAX_SI50_ENTRIES * size);
            //On Win9x, must allocate buffer before calling API
            IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer);

            try
            {
                nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
                                    out entriesRead, out totalEntries);

                if (ERROR_WRONG_LEVEL == nRet)
                {
                    level = 1;
                    t     = typeof(SHARE_INFO_1_9x);
                    size  = Marshal.SizeOf(t);

                    nRet = NetShareEnum(server, level, pBuffer, cbBuffer,
                                        out entriesRead, out totalEntries);
                }

                if (NO_ERROR == nRet || ERROR_MORE_DATA == nRet)
                {
                    for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += size)
                    {
                        IntPtr pItem = new IntPtr(lpItem);

                        if (1 == level)
                        {
                            SHARE_INFO_1_9x si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                        }
                        else
                        {
                            SHARE_INFO_50 si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                        }
                    }
                }
                else
                {
                    Console.WriteLine(nRet);
                }
            }
            finally
            {
                //Clean up buffer
                Marshal.FreeHGlobal(pBuffer);
            }
        }
コード例 #4
0
        /// <summary>
        /// Enumerates the shares on Windows NT
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateSharesNT(string server, ShareCollection shares)
        {
            int    level = 2;
            int    entriesRead, totalEntries, nRet, hResume = 0;
            IntPtr pBuffer = IntPtr.Zero;

            try
            {
                nRet = NetShareEnum(server, level, out pBuffer, -1,
                                    out entriesRead, out totalEntries, ref hResume);

                if (ERROR_ACCESS_DENIED == nRet)
                {
                    //Need admin for level 2, drop to level 1
                    level = 1;
                    nRet  = NetShareEnum(server, level, out pBuffer, -1,
                                         out entriesRead, out totalEntries, ref hResume);
                }

                if (NO_ERROR == nRet && entriesRead > 0)
                {
                    Type t      = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1);
                    int  offset = Marshal.SizeOf(t);

                    IntPtr pItem = pBuffer;

                    for (long i = 0; i < entriesRead; i++)
                    {
                        if (1 == level)
                        {
                            SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                        }
                        else
                        {
                            SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                        }

                        pItem = IntPtr.Add(pItem, offset);
                    }
                }
            }
            finally
            {
                // Clean up buffer allocated by system
                if (IntPtr.Zero != pBuffer)
                {
                    NetApiBufferFree(pBuffer);
                }
            }
        }
コード例 #5
0
 protected static void EnumerateShares(string server, ShareCollection shares)
 {
     if (null != server && 0 != server.Length && !IsW2KUp)
     {
         server = server.ToUpper();
         if (!('\\' == server[0] && '\\' == server[1]))
         {
             server = @"\\" + server;
         }
     }
     if (IsNT)
     {
         EnumerateSharesNT(server, shares);
     }
     else
     {
         EnumerateShares9x(server, shares);
     }
 }
コード例 #6
0
        protected static void EnumerateSharesNT(string server, ShareCollection shares)
        {
            int    level = 2;
            int    entriesRead, totalEntries, nRet, hResume = 0;
            IntPtr pBuffer = IntPtr.Zero;

            try
            {
                nRet = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume);
                if (ERROR_ACCESS_DENIED == nRet)
                {
                    level = 1;
                    nRet  = NetShareEnum(server, level, out pBuffer, -1, out entriesRead, out totalEntries, ref hResume);
                }
                if (NO_ERROR == nRet && entriesRead > 0)
                {
                    Type t      = (2 == level)?typeof(SHARE_INFO_2):typeof(SHARE_INFO_1);
                    int  offset = Marshal.SizeOf(t);
                    for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset)
                    {
                        IntPtr pItem = new IntPtr(lpItem);
                        if (1 == level)
                        {
                            SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                        }
                        else
                        {
                            SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
                            shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                        }
                    }
                }
            }
            finally
            {
                if (IntPtr.Zero != pBuffer)
                {
                    NetApiBufferFree(pBuffer);
                }
            }
        }
コード例 #7
0
ファイル: Shares.cs プロジェクト: sillsdev/WorldPad
        /// <summary>
        /// Enumerates the shares
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateShares(string server, ShareCollection shares, ref bool accessDenied)
        {
            if (null != server && 0 != server.Length && !IsW2KUp)
            {
                server = server.ToUpperInvariant();

                // On NT4, 9x and Me, server has to start with "\\"
                if (!('\\' == server[0] && '\\' == server[1]))
                {
                    server = @"\\" + server;
                }
            }
            accessDenied = false;

            if (IsNT)
            {
                EnumerateSharesNT(server, shares, ref accessDenied);
            }
            else
            {
                EnumerateShares9x(server, shares, ref accessDenied);
            }
        }
コード例 #8
0
        public List <DirectoryInfo> GetComputerShares(string server)
        {
            List <DirectoryInfo> computerShares = new List <DirectoryInfo>();

            ShareCollection shareCollection;

            if (server != null && server.Trim().Length > 0)
            {
                shareCollection = ShareCollection.GetShares(server);
                if (shareCollection != null)
                {
                    foreach (Share si in shareCollection)
                    {
                        // If this is a file-system share, try to list the first five subfolders.
                        // NB: If the share is on a removable device, you could get "Not ready" or "Access denied" exceptions.
                        // If you don't have permissions to the share, you will get security exceptions.
                        if (si.IsFileSystem)
                        {
                            try
                            {
                                System.IO.DirectoryInfo d = si.Root;
                                d.EnumerateFiles();
                                //System.IO.DirectoryInfo[] Flds = d.GetDirectories();
                                computerShares.Add(si.Root);
                            }
                            catch                             //(Exception ex)
                            {
                                //Access denined - Console.WriteLine("\tError listing {0}:\n\t{1}\n", si.ToString(), ex.Message);
                            }
                        }
                    }
                }
                // else "Unable to enumerate the shares on {0}.\n" + "Make sure the machine exists, and that you have permission to access it.", server);
            }

            return(computerShares);
        }
コード例 #9
0
        /// <summary>
        /// Enumerates the shares
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateShares(string server, ShareCollection shares)
        {
            if (null != server && 0 != server.Length && !IsW2KUp)
            {
                server = server.ToUpper();

                // On NT4, 9x and Me, server has to start with "\\"
                if (!('\\' == server[0] && '\\' == server[1]))
                {
                    server = @"\\" + server;
                }
            }

            if (Environment.Is64BitProcess)
            {
                if (IsNT)
                {
                    EnumerateSharesNT64(server, shares);
                }
                else
                {
                    EnumerateShares9x64(server, shares);
                }
            }
            else
            {
                if (IsNT)
                {
                    EnumerateSharesNT(server, shares);
                }
                else
                {
                    EnumerateShares9x(server, shares);
                }
            }
        }
コード例 #10
0
ファイル: Shares.cs プロジェクト: sillsdev/WorldPad
        /// <summary>
        /// Returns the UNC path for a mapped drive or local share.
        /// </summary>
        /// <param name="fileName">The path to map</param>
        /// <returns>The UNC path (if available)</returns>
        public static string PathToUnc(string fileName)
        {
            if (null == fileName || 0 == fileName.Length)
            {
                return(string.Empty);
            }

            fileName = Path.GetFullPath(fileName);
            if (!IsValidFilePath(fileName))
            {
                return(fileName);
            }

            int nRet = 0;
            UNIVERSAL_NAME_INFO rni = new UNIVERSAL_NAME_INFO();
            int bufferSize          = Marshal.SizeOf(rni);

            nRet = WNetGetUniversalName(
                fileName, UNIVERSAL_NAME_INFO_LEVEL,
                ref rni, ref bufferSize);

            if (ERROR_MORE_DATA == nRet)
            {
                IntPtr pBuffer = Marshal.AllocHGlobal(bufferSize);;
                try
                {
                    nRet = WNetGetUniversalName(
                        fileName, UNIVERSAL_NAME_INFO_LEVEL,
                        pBuffer, ref bufferSize);

                    if (NO_ERROR == nRet)
                    {
                        rni = (UNIVERSAL_NAME_INFO)Marshal.PtrToStructure(pBuffer,
                                                                          typeof(UNIVERSAL_NAME_INFO));
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(pBuffer);
                }
            }

            switch (nRet)
            {
            case NO_ERROR:
                return(rni.lpUniversalName);

            case ERROR_NOT_CONNECTED:
                //Local file-name
                ShareCollection shi = LocalShares;
                if (null != shi)
                {
                    Share share = shi[fileName];
                    if (null != share)
                    {
                        string path = share.Path;
                        if (null != path && 0 != path.Length)
                        {
                            int index = path.Length;
                            if (Path.DirectorySeparatorChar != path[path.Length - 1])
                            {
                                index++;
                            }

                            if (index < fileName.Length)
                            {
                                fileName = fileName.Substring(index);
                            }
                            else
                            {
                                fileName = string.Empty;
                            }

                            fileName = Path.Combine(share.ToString(), fileName);
                        }
                    }
                }

                return(fileName);

            default:
                Console.WriteLine("Unknown return value: {0}", nRet);
                return(string.Empty);
            }
        }
コード例 #11
0
ファイル: Shares.cs プロジェクト: bgr/bgrCopySharePath
        /// <summary>
        /// Enumerates the shares on Windows NT
        /// </summary>
        /// <param name="server">The server name</param>
        /// <param name="shares">The ShareCollection</param>
        protected static void EnumerateSharesNT(string server, ShareCollection shares)
        {
            int    level = 2;
            int    entriesRead, totalEntries, nRet, hResume = 0;
            IntPtr pBuffer = IntPtr.Zero;

            try
            {
                nRet = NetShareEnum(server, level, out pBuffer, -1,
                                    out entriesRead, out totalEntries, ref hResume);

                if (ERROR_ACCESS_DENIED == nRet)
                {
                    //Need admin for level 2, drop to level 1
                    level = 1;
                    nRet  = NetShareEnum(server, level, out pBuffer, -1,
                                         out entriesRead, out totalEntries, ref hResume);
                }

                if (NO_ERROR == nRet && entriesRead > 0)
                {
                    Type t      = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1);
                    int  offset = Marshal.SizeOf(t);

                    for (int i = 0, lpItem = pBuffer.ToInt32(); i < entriesRead; i++, lpItem += offset)
                    {
                        IntPtr pItem = new IntPtr(lpItem);
                        if (1 == level)
                        {
                            SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
                            if (si.ShareType == ShareType.Special && si.NetName.Length == 2)
                            {
                            }
                            else
                            {
                                shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
                                //Console.WriteLine(trim(si.NetName) + " no path         " + si.ShareType + "\t" + si.Remark);
                            }
                        }
                        else
                        {
                            SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
                            if (si.ShareType == ShareType.Special && si.NetName.Length == 2 && si.Path.Length == 3)
                            {
                            }
                            else
                            {
                                shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
                                //Console.Write(trim(si.NetName) + " " + trim(si.Path) + " " + si.ShareType + "\t" + si.Remark + "\n");
                            }
                        }
                    }
                }
            }
            finally
            {
                // Clean up buffer allocated by system
                if (IntPtr.Zero != pBuffer)
                {
                    NetApiBufferFree(pBuffer);
                }
            }
        }
コード例 #12
0
ファイル: Shares.cs プロジェクト: hrblxp/system-config
		/// <summary>
		/// Enumerates the shares
		/// </summary>
		/// <param name="server">The server name</param>
		/// <param name="shares">The ShareCollection</param>
		protected static void EnumerateShares(string server, ShareCollection shares)
		{
			if (null != server && 0 != server.Length && !IsW2KUp) 
			{
				server = server.ToUpper();
				
				// On NT4, 9x and Me, server has to start with "\\"
				if (!('\\' == server[0] && '\\' == server[1])) 
					server = @"\\" + server;
			}
		
			if (IsNT)
				EnumerateSharesNT(server, shares);
			else
				EnumerateShares9x(server, shares);
		}
コード例 #13
0
ファイル: Shares.cs プロジェクト: hrblxp/system-config
		/// <summary>
		/// Enumerates the shares on Windows 9x
		/// </summary>
		/// <param name="server">The server name</param>
		/// <param name="shares">The ShareCollection</param>
		protected static void EnumerateShares9x(string server, ShareCollection shares)
		{
			int level = 50;
			int nRet = 0;
			ushort entriesRead, totalEntries;
			
			Type t = typeof(SHARE_INFO_50);
			int size = Marshal.SizeOf(t);
			ushort cbBuffer = (ushort)(MAX_SI50_ENTRIES * size);
			//On Win9x, must allocate buffer before calling API
			IntPtr pBuffer = Marshal.AllocHGlobal(cbBuffer);

			try 
			{
				nRet = NetShareEnum(server, level, pBuffer, cbBuffer, 
					out entriesRead, out totalEntries);
				
				if (ERROR_WRONG_LEVEL == nRet)
				{
					level = 1;
					t = typeof(SHARE_INFO_1_9x);
					size = Marshal.SizeOf(t);
					
					nRet = NetShareEnum(server, level, pBuffer, cbBuffer, 
						out entriesRead, out totalEntries);
				}

				if (NO_ERROR == nRet || ERROR_MORE_DATA == nRet) 
				{
					for (int i=0, lpItem=pBuffer.ToInt32(); i<entriesRead; i++, lpItem+=size) 
					{
						IntPtr pItem = new IntPtr(lpItem);
						
						if (1 == level)
						{
							SHARE_INFO_1_9x si = (SHARE_INFO_1_9x)Marshal.PtrToStructure(pItem, t);
							shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
						}
						else
						{
							SHARE_INFO_50 si = (SHARE_INFO_50)Marshal.PtrToStructure(pItem, t);
							shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
						}
					}
				}
				else
					Console.WriteLine(nRet);
				
			}
			finally 
			{
				//Clean up buffer
				Marshal.FreeHGlobal(pBuffer);
			}
		}
コード例 #14
0
ファイル: Shares.cs プロジェクト: hrblxp/system-config
		/// <summary>
		/// Enumerates the shares on Windows NT
		/// </summary>
		/// <param name="server">The server name</param>
		/// <param name="shares">The ShareCollection</param>
		protected static void EnumerateSharesNT(string server, ShareCollection shares)
		{
			int level = 2;
			int entriesRead, totalEntries, nRet, hResume = 0;
			IntPtr pBuffer = IntPtr.Zero;

			try 
			{
				nRet = NetShareEnum(server, level, out pBuffer, -1, 
					out entriesRead, out totalEntries, ref hResume);

				if (ERROR_ACCESS_DENIED == nRet) 
				{
					//Need admin for level 2, drop to level 1
					level = 1;
					nRet = NetShareEnum(server, level, out pBuffer, -1, 
						out entriesRead, out totalEntries, ref hResume);
				}

				if (NO_ERROR == nRet && entriesRead > 0) 
				{
					Type t = (2 == level) ? typeof(SHARE_INFO_2) : typeof(SHARE_INFO_1);
					int offset = Marshal.SizeOf(t);

					for (int i=0, lpItem=pBuffer.ToInt32(); i<entriesRead; i++, lpItem+=offset) 
					{
						IntPtr pItem = new IntPtr(lpItem);
						if (1 == level) 
						{
							SHARE_INFO_1 si = (SHARE_INFO_1)Marshal.PtrToStructure(pItem, t);
							shares.Add(si.NetName, string.Empty, si.ShareType, si.Remark);
						}
						else 
						{
							SHARE_INFO_2 si = (SHARE_INFO_2)Marshal.PtrToStructure(pItem, t);
							shares.Add(si.NetName, si.Path, si.ShareType, si.Remark);
						}
					}
				}
				
			}
			finally 
			{
				// Clean up buffer allocated by system
				if (IntPtr.Zero != pBuffer) 
					NetApiBufferFree(pBuffer);
			}
		}
コード例 #15
0
ファイル: Shares.cs プロジェクト: sillsdev/WorldPad
		/// <summary>
		/// Enumerates the shares
		/// </summary>
		/// <param name="server">The server name</param>
		/// <param name="shares">The ShareCollection</param>
		protected static void EnumerateShares(string server, ShareCollection shares, ref bool accessDenied)
		{
			if (null != server && 0 != server.Length && !IsW2KUp)
			{
				server = server.ToUpperInvariant();

				// On NT4, 9x and Me, server has to start with "\\"
				if (!('\\' == server[0] && '\\' == server[1]))
					server = @"\\" + server;
			}
			accessDenied = false;

			if (IsNT)
				EnumerateSharesNT(server, shares,ref  accessDenied);
			else
				EnumerateShares9x(server, shares,ref  accessDenied);
		}