示例#1
0
        private static string GetEnvironmentVariableCore(string variable)
        {
            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if the variable was not found in this case.
                return(null);
            }

            StringBuilder sb           = StringBuilderCache.Acquire(128); // A somewhat reasonable default size
            int           requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);

            if (requiredSize == 0 && Marshal.GetLastWin32Error() == Win32Native.ERROR_ENVVAR_NOT_FOUND)
            {
                StringBuilderCache.Release(sb);
                return(null);
            }

            while (requiredSize > sb.Capacity)
            {
                sb.Capacity  = requiredSize;
                sb.Length    = 0;
                requiredSize = Win32Native.GetEnvironmentVariable(variable, sb, sb.Capacity);
            }

            return(StringBuilderCache.GetStringAndRelease(sb));
        }
示例#2
0
        private static void SetEnvironmentVariableCore(string variable, string value)
        {
            // explicitly null out value if is the empty string.
            if (string.IsNullOrEmpty(value) || value[0] == '\0')
            {
                value = null;
            }

            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // so we throw PlatformNotSupportedException.
                throw new PlatformNotSupportedException();
            }

            if (!Win32Native.SetEnvironmentVariable(variable, value))
            {
                int errorCode = Marshal.GetLastWin32Error();

                switch (errorCode)
                {
                case Win32Native.ERROR_ENVVAR_NOT_FOUND:
                    // Allow user to try to clear a environment variable
                    return;

                case Win32Native.ERROR_FILENAME_EXCED_RANGE:
                    // The error message from Win32 is "The filename or extension is too long",
                    // which is not accurate.
                    throw new ArgumentException(GetResourceString("Argument_LongEnvVarValue"));

                default:
                    throw new ArgumentException(Win32Native.GetMessage(errorCode));
                }
            }
        }
示例#3
0
        private static IDictionary GetEnvironmentVariablesCore()
        {
            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if no environment variables are defined in this case.
                return(new Dictionary <string, string>(0));
            }

            return(GetRawEnvironmentVariables());
        }
示例#4
0
        public static String ExpandEnvironmentVariables(String name)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }
            Contract.EndContractBlock();

            if (name.Length == 0)
            {
                return(name);
            }

            if (AppDomain.IsAppXModel() && !AppDomain.IsAppXDesignMode())
            {
                // Environment variable accessors are not approved modern API.
                // Behave as if no variables are defined in this case.
                return(name);
            }

            int           currentSize = 100;
            StringBuilder blob        = new StringBuilder(currentSize); // A somewhat reasonable default size

#if PLATFORM_UNIX                                                       // Win32Native.ExpandEnvironmentStrings isn't available
            int lastPos = 0, pos;
            while (lastPos < name.Length && (pos = name.IndexOf('%', lastPos + 1)) >= 0)
            {
                if (name[lastPos] == '%')
                {
                    string key   = name.Substring(lastPos + 1, pos - lastPos - 1);
                    string value = Environment.GetEnvironmentVariable(key);
                    if (value != null)
                    {
                        blob.Append(value);
                        lastPos = pos + 1;
                        continue;
                    }
                }
                blob.Append(name.Substring(lastPos, pos - lastPos));
                lastPos = pos;
            }
            blob.Append(name.Substring(lastPos));
#else
            int size;

            blob.Length = 0;
            size        = Win32Native.ExpandEnvironmentStrings(name, blob, currentSize);
            if (size == 0)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            while (size > currentSize)
            {
                currentSize   = size;
                blob.Capacity = currentSize;
                blob.Length   = 0;

                size = Win32Native.ExpandEnvironmentStrings(name, blob, currentSize);
                if (size == 0)
                {
                    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                }
            }
#endif // PLATFORM_UNIX

            return(blob.ToString());
        }