Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# generate random key with specified length

 /// <summary>
        /// Generate Random Key
        /// </summary>
        /// <param name="length">Lenght of key to generate</param>
        /// <returns></returns>
        public static string UniqueKey(int length)
        {
            string guidResult = string.Empty;

            while (guidResult.Length < length)
            {
                // Get the GUID.
                guidResult += Guid.NewGuid().ToString().GetHashCode().ToString("#");
            }

            // Make sure length is valid.
            if (length <= 0 || length > guidResult.Length)
                throw new ArgumentException("Length must be between 1 and " + guidResult.Length);

            // Return the first length bytes.
            return guidResult.Substring(0, length);
        }
 
PREVIOUS NEXT
Tagged: #generate #random #key #length
ADD COMMENT
Topic
Name
1+5 =