/// <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); } }
/// <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); } } }
/// <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); } }
/// <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]); } }
public override Innovator.Client.IPromise <CompletionContext> ShowCompletions(EditorWinForm control) { var context = new CompletionContext(); try { var caret = control.Editor.CaretOffset; var text = control.Editor.Text; var current = text.Substring(0, caret); var lastMacro = current.LastIndexOf('$'); var preMacro = lastMacro > 0 ? current.Substring(0, lastMacro).Trim() : current; if (lastMacro >= 0 && current.LastIndexOf(')') < lastMacro) { var overlap = current.Substring(lastMacro); var filter = overlap.TrimStart('$', '('); var items = Macros.Select(m => new BasicCompletionData() { Text = m, Action = () => "(" + m + ")", Image = Icons.Operator16.Wpf }) .Where(i => i.Text.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0) .OrderBy(i => i.Text); context.Items = items; control.ShowCompletionWindow(items, overlap.Length - 1); } else if (File.Exists(preMacro)) { var items = Macros.Select(m => new BasicCompletionData() { Text = m, Action = () => "$(" + m + ")", Image = Icons.Operator16.Wpf }) .OrderBy(i => i.Text); context.Items = items; control.ShowCompletionWindow(items, 0); } else if (current.Length >= 3) { var dir = (current.EndsWith("/") || current.EndsWith("\\")) ? current : Path.GetDirectoryName(current) + "\\"; var overlap = current.Length > dir.Length ? current.Substring(dir.Length) : string.Empty; IEnumerable <ICompletionData> items; if (dir.StartsWith(@"\\") && dir.Trim('\\').IndexOf(@"\") < 0) { items = ShareCollection.GetShares(dir) .Select(s => new BasicCompletionData() { Text = s.NetName, Image = Icons.Folder16.Wpf }); } else { items = new DirectoryInfo(dir).GetFileSystemInfos() .Select(e => new BasicCompletionData() { Text = e.Name, Image = (e.Attributes & FileAttributes.Directory) > 0 ? Icons.Folder16.Wpf : Icons.EnumValue16.Wpf }); } items = items .Where(i => i.Text.IndexOf(overlap, StringComparison.OrdinalIgnoreCase) >= 0) .OrderBy(i => i.Text); context.Items = items; control.ShowCompletionWindow(items, overlap.Length); } } catch (IOException) { } catch (ArgumentException) { } return(Innovator.Client.Promises.Resolved(context)); }
/// <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); } }