System.Resources.UriUtility.UriEncode C# (CSharp) Method

UriEncode() public static method

public static UriEncode ( string str ) : string
str string
return string
        public static string UriEncode(string str)
        {
            if (str == null)
                return null;

            byte[] bytes = Encoding.UTF8.GetBytes(str);

            int count = bytes.Length;

            if (count == 0)
                return null;

            int cBytesToEncode = 0;

            // count them first
            for (int i = 0; i < count; i++)
            {
                if (!IsUriUnreservedChar((char)bytes[i]))
                    cBytesToEncode++;
            }

            byte[] expandedBytes = null;

            // nothing to expand?
            if (cBytesToEncode == 0)
                expandedBytes = bytes;
            else
            {
                // expand not 'safe' characters into %xx
                expandedBytes = new byte[count + cBytesToEncode * 2];
                int pos = 0;

                for (int i = 0; i < count; i++)
                {
                    byte b = bytes[i];

                    if (IsUriUnreservedChar((char)b))
                    {
                        expandedBytes[pos++] = b;
                    }
                    else
                    {
                        expandedBytes[pos++] = (byte)'%';
                        expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf);
                        expandedBytes[pos++] = (byte)IntToHex(b & 0xf);
                    }
                }
            }

            // Since all the bytes in expandedBytes are ascii characters UTF8 encoding can be used
            return Encoding.UTF8.GetString(expandedBytes);
        }

Usage Example

        // Obtains a resource string given the name of the string resource, and optional starting
        // and neutral resource cultures (e.g. "de-DE").

        // Thread-safe provided that the call to Initialize() happened only once on one thread
        // and has already completed successfully, and that the WinRT APIs called below
        // continue to be thread-safe.

        // Throws exceptions
        public override String GetString(String stringName,
                                         String startingCulture, String neutralResourcesCulture)
        {
            Debug.Assert(stringName != null);
            Debug.Assert(_resourceMap != null); // Should have been initialized by now

            ResourceCandidate resourceCandidate = null;

            stringName = UriUtility.UriEncode(stringName);

            if (startingCulture == null && neutralResourcesCulture == null)
            {
#pragma warning disable 618
                resourceCandidate = _resourceMap.GetValue(stringName);
#pragma warning restore 618
            }
            else
            {
                Debug.Assert(_clonedResourceContext != null);                 // Should have been initialized by now
                Debug.Assert(_clonedResourceContextFallBackList != null);     // Should have been initialized by now
                Debug.Assert(s_globalResourceContextFallBackList != null);    // Should have been initialized by now
                Debug.Assert(s_globalResourceContextFallBackList.Length > 0); // Should never be empty

                // We need to modify the culture fallback list used by the Modern Resource Manager
                // The starting culture has to be looked up first, and neutral resources culture has
                // to be looked up last.

                string newResourceFallBackList = null;

                newResourceFallBackList =
                    (startingCulture == null ? "" : startingCulture + ";") +
                    s_globalResourceContextFallBackList +    // Our tests do not test this line of code, so be extra careful if you modify or move it.
                    (neutralResourcesCulture == null ? "" : ";" + neutralResourcesCulture);

                lock (_clonedResourceContext)
                {
                    // s_globalResourceContextFallBackList may have changed on another thread by now.
                    // We cannot prevent that from happening because it may have happened on a native
                    // thread, and we do not share the same lock mechanisms with native code.
                    // The worst that can happen is that a string is unexpectedly missing
                    // or in the wrong language.

                    if (!String.Equals(newResourceFallBackList, _clonedResourceContextFallBackList, StringComparison.Ordinal))
                    {
                        _clonedResourceContext.Languages   = StringToReadOnlyList(newResourceFallBackList);
                        _clonedResourceContextFallBackList = newResourceFallBackList;
                    }

                    resourceCandidate = _resourceMap.GetValue(stringName, _clonedResourceContext);
                }
            }

            if (resourceCandidate != null)
            {
                return(resourceCandidate.ValueAsString);
            }

            return(null);
        }
All Usage Examples Of System.Resources.UriUtility::UriEncode