Esempio n. 1
0
        /// <summary>
        /// Retrieves all defined aliases for the specified executable.
        /// </summary>
        /// <param name="ExeName">The executable file whose aliases are to be retrieved.</param>
        /// <returns>Returns a NameValueCollection that contains the source and target
        /// (replacement) strings for all defined aliases.</returns>
        public static NameValueCollection GetAliases(string ExeName)
        {
            int length = GetAliasesLength(ExeName);
            NameValueCollection aliases = new NameValueCollection();

            if (length > 0)
            {
                char[] buff = new char[length];
                if (WinCon.GetConsoleAliases(buff, length, ExeName) == 0)
                {
                    throw new IOException("Unable to retrieve alias strings", Marshal.GetLastWin32Error());
                }
                // The returned buffer contains a series of nul-terminated strings
                // of the form source=target\0source=target\0
                // Parse the strings and add them to the aliases collection.
                int    startIndex = 0;
                string source     = "";
                string target     = "";
                bool   bSource    = true;
                for (int i = 0; i < length; i++)
                {
                    if (bSource)
                    {
                        // searching for source string
                        if (buff[i] == '=')
                        {
                            source     = new string(buff, startIndex, i - startIndex);
                            startIndex = i + 1;
                            bSource    = false;
                        }
                    }
                    else
                    {
                        // searching for target string
                        if (buff[i] == '\0')
                        {
                            target     = new string(buff, startIndex, i - startIndex);
                            startIndex = i + 1;
                            aliases.Add(source, target);
                            bSource = true;
                        }
                    }
                }
            }
            return(aliases);
        }