Code that check for the existence of the server’s named pipe in C#


From this MSDN link


[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool WaitNamedPipe(string name, int timeout);
public static bool namedPipeExist(string pipeName)
        int timeout = 0;
        string normalizedPath = System.IO.Path.GetFullPath(
            string.Format(@"\\.\pipe\{0}", pipeName));
        bool exists = WaitNamedPipe(normalizedPath, timeout);
        if (!exists)
            int error = Marshal.GetLastWin32Error();
            if (error == 0) // pipe does not exist
                return false;
            else if (error == 2) // win32 error code for file not found
                return false;
            // all other errors indicate other issues
        return true;
    catch (Exception)
        return false; // assume it doesn't exist

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s