How can I get a list of all open named pipes in Windows and avoid possible exceptions?

retrieving a list of named pipes is ideally quite simple and can be found here: How can I get a list of all open named pipes in Windows?

But the mentioned solution

var namedPipes = Directory.GetFiles(@"\\.\pipe\"); 

has unpredictable results. One of them was mentioned in the link above (Invalid character in path exception). Today I met my own exception - ArgumentException "The second fragment of the path should not be a disk name or UNC. Parameter name: path2".

Question: is there really a working solution in .net for the gett list of all open named pipes? Thanks

+2
c # named-pipes
Aug 03
source share
1 answer

I dug up the source code for the Directory class and found inspiration. Here is a working solution that gives you a list of all open named pipes. My result does not contain the prefix \\. \ Pipe \, as can be seen from the result of Directory.GetFiles. I tested my solution on WinXp SP3, Win 7, Win 8.1.

  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] struct WIN32_FIND_DATA { public uint dwFileAttributes; public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime; public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime; public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime; public uint nFileSizeHigh; public uint nFileSizeLow; public uint dwReserved0; public uint dwReserved1; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string cFileName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)] public string cAlternateFileName; } [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData); [DllImport("kernel32.dll", SetLastError = true)] static extern bool FindClose(IntPtr hFindFile); private static void Main(string[] args) { var namedPipes = new List<string>(); WIN32_FIND_DATA lpFindFileData; var ptr = FindFirstFile(@"\\.\pipe\*", out lpFindFileData); namedPipes.Add(lpFindFileData.cFileName); while (FindNextFile(ptr, out lpFindFileData)) { namedPipes.Add(lpFindFileData.cFileName); } FindClose(ptr); namedPipes.Sort(); foreach (var v in namedPipes) Console.WriteLine(v); Console.ReadLine(); } 
+7
Aug 04 '14 at 20:23
source share



All Articles