123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Dynamic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Web;
-
- namespace Teknik
- {
- public static class Utility
- {
- public static dynamic Merge(object item1, object item2)
- {
- if (item1 == null || item2 == null)
- return item1 ?? item2 ?? new ExpandoObject();
-
- dynamic expando = new ExpandoObject();
- var result = expando as IDictionary<string, object>;
- foreach (System.Reflection.PropertyInfo fi in item1.GetType().GetProperties())
- {
- result[fi.Name] = fi.GetValue(item1, null);
- }
- foreach (System.Reflection.PropertyInfo fi in item2.GetType().GetProperties())
- {
- result[fi.Name] = fi.GetValue(item2, null);
- }
- return result;
- }
-
- public static string RandomString(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
- {
- const int byteSize = 0x100;
- var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
- if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize));
-
- // Guid.NewGuid and System.Random are not particularly random. By using a
- // cryptographically-secure random number generator, the caller is always
- // protected, regardless of use.
- using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
- {
- var result = new StringBuilder();
- var buf = new byte[128];
- while (result.Length < length)
- {
- rng.GetBytes(buf);
- for (var i = 0; i < buf.Length && result.Length < length; ++i)
- {
- // Divide the byte into allowedCharSet-sized groups. If the
- // random value falls into the last group and the last group is
- // too small to choose from the entire allowedCharSet, ignore
- // the value in order to avoid biasing the result.
- var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
- if (outOfRangeStart <= buf[i]) continue;
- result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
- }
- }
- return result.ToString();
- }
- }
-
- public static string GenerateUniqueFileName(string path, string extension, int length)
- {
- if (Directory.Exists(path))
- {
- string filename = RandomString(length);
- string subDir = filename[0].ToString();
- path = Path.Combine(path, subDir);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- while (File.Exists(Path.Combine(path, string.Format("{0}.{1}", filename, extension))))
- {
- filename = RandomString(length);
- subDir = filename[0].ToString();
- path = Path.Combine(path, subDir);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- }
-
- return Path.Combine(path, string.Format("{0}.{1}", filename, extension));
- }
-
- return string.Empty;
- }
-
- public static string GetDefaultExtension(string mimeType, string defaultExtension = "")
- {
- string result;
- RegistryKey key;
- object value;
-
- key = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + mimeType, false);
- value = key != null ? key.GetValue("Extension", null) : null;
- result = value != null ? value.ToString() : defaultExtension;
-
- return result;
- }
-
- public static string GetBytesReadable(long i)
- {
- // Get absolute value
- long absolute_i = (i < 0 ? -i : i);
- // Determine the suffix and readable value
- string suffix;
- double readable;
- if (absolute_i >= 0x1000000000000000) // Exabyte
- {
- suffix = "EB";
- readable = (i >> 50);
- }
- else if (absolute_i >= 0x4000000000000) // Petabyte
- {
- suffix = "PB";
- readable = (i >> 40);
- }
- else if (absolute_i >= 0x10000000000) // Terabyte
- {
- suffix = "TB";
- readable = (i >> 30);
- }
- else if (absolute_i >= 0x40000000) // Gigabyte
- {
- suffix = "GB";
- readable = (i >> 20);
- }
- else if (absolute_i >= 0x100000) // Megabyte
- {
- suffix = "MB";
- readable = (i >> 10);
- }
- else if (absolute_i >= 0x400) // Kilobyte
- {
- suffix = "KB";
- readable = i;
- }
- else
- {
- return i.ToString("0 B"); // Byte
- }
- // Divide by 1024 to get fractional value
- readable = (readable / 1024);
- // Return formatted number with suffix
- return readable.ToString("0.### ") + suffix;
- }
- }
- }
|