示例#1
0
        /// <summary>
        /// Retrieves the text for the specified console alias and executable.
        /// </summary>
        /// <param name="Source">The console alias whose text is to be retrieved.</param>
        /// <param name="ExeName">The name of the executable file.</param>
        /// <returns>On success, the return value is a non-null, non-empty string.
        /// If the Source and ExeName do not specify a valid alias, null is returned.</returns>
        public static string GetAlias(string Source, string ExeName)
        {
            // The only reliable way to allocate enough space is to the
            // combined length for all aliases.
            int length = GetAliasesLength(ExeName);

            if (length == 0)
            {
                // no aliases defined, so just return null
                return(null);
            }

            char[] buff = new char[length];
            int    rslt = WinCon.GetConsoleAlias(Source, buff, buff.Length, ExeName);

            if (rslt == 0)
            {
                int err = Marshal.GetLastWin32Error();
                // GetConsoleAlias fails if it can't find the alias.
                // GetLastError returns 31 in that case.
                if (err == 31) // ERROR_GEN_FAILURE
                {
                    return(null);
                }
                throw new IOException("Unable to get alias", err);
            }

            // Documentation for GetConsoleAlias says that the return value is non-zero
            // on success.  It appears that the return value is the number of characters
            // copied to the buffer (including the 0 terminator), but I can't be
            // sure that's reliable.
            // go look for the terminator.
            int i;

            for (i = 0; i < buff.Length; i++)
            {
                if (buff[i] == '\0')
                {
                    break;
                }
            }

            return(new string(buff, 0, i));
        }