示例#1
0
        /// <summary>
        /// Creates a file of a specified size at a given location containing cryptographically randomized data.
        /// </summary>
        /// <param name="path">The location which the file should be created.</param>
        /// <param name="size">The size which the created size will be relative to the chosen <see cref="FileSizeType"/>.</param>
        /// <param name="type">The size type which determines how large the created file will be.</param>
        /// <returns>A flag determining whether the operation was successful.</returns>
        public async Task <bool> GenerateFileAsync(string path, int size, FileSizeType type)
        {
            try
            {
                if (!File.Exists(path))
                {
                    // Create our default buffer and block sizes.
                    //float totalBytes = 0;
                    //float bytesWritten = 0;
                    int bufferSize = 1024 * 1024;
                    int blockSize  = (1024) / bufferSize;

                    // Check our size type and calculate the actual buffer and block length.
                    switch (type)
                    {
                    case FileSizeType.Kilobytes:
                        bufferSize = 1024;
                        blockSize  = (1024) / bufferSize;
                        break;

                    case FileSizeType.Megabytes:
                        blockSize = (1024 * 1024) / bufferSize;
                        break;

                    case FileSizeType.Gigabytes:
                        blockSize = (1024 * 1024 * 1024) / bufferSize;
                        break;

                    default:
                        return(false);
                    }

                    // Generate random data and write it to disk.
                    using (var file = LongFile.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        byte[] data = new byte[bufferSize];
                        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                        //totalBytes = (size * blockSize) * bufferSize;
                        for (int i = 0; i < size * blockSize; i++)
                        {
                            rng.GetBytes(data);
                            await file.WriteAsync(data, 0, data.Length, CancelToken);

                            //bytesWritten += data.Length;
                            //float progress = ((bytesWritten / totalBytes) * 100);
                            //OnGenerateFileProgressChanged(new GenerateFileProgressChangedEventArgs((int)(progress)));
                        }
                        file.Flush();
                        file.Close();
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch { return(false); }
        }
示例#2
0
 public AzureFileData(string blob, string friendlyName, string azureUrl, FileSizeType type)
 {
     BlobName         = blob;
     FriendlyFileName = friendlyName;
     Url          = azureUrl;
     FileSizeType = type;
 }
示例#3
0
        private void Initialize(long size, FileSizeType fileSizeType)
        {
            switch (fileSizeType)
            {
            case FileSizeType.Bit:
                bytes = size / BitsInByte;
                break;

            case FileSizeType.Byte:
                bytes = size;
                break;

            case FileSizeType.KiloByte:
                bytes = size * BytesInKilobyte;
                break;

            case FileSizeType.MegaByte:
                bytes = size * BytesInMegabyte;
                break;

            case FileSizeType.GigaByte:
                bytes = size * BytesInGigabyte;
                break;

            case FileSizeType.TeraByte:
                bytes = size * BytesInTerabyte;
                break;
            }
        }
        private SelectList fillSizeTypeList()
        {
            var values = from FileSizeType e in FileSizeType.GetValues(typeof(FileSizeType))
                         select new { Id = e, Name = e.ToString() };

            return(new SelectList(values, "Id", "Name"));
            //model.SizeType = new SelectList(new List<FileSizeType>());
        }
示例#5
0
        /// <summary>
        /// Reduce a filesize (in kb) to the maximum target number of leading signifigant digits
        /// </summary>
        public static double GetReducedSize(long size, int targetNumberOfDigits, out FileSizeType fileSizeType)
        {
            long ReducedSize = size;

            var FileSizeTypes = Enum.GetValues <FileSizeType>().Where(x => size > x.GetCustomAttribute <ValueAttribute>().Value);

            fileSizeType = FileSizeTypes.Any() ? FileSizeTypes.Max() : FileSizeType.Kb;

            long FileSizeTypeModifier = fileSizeType.GetCustomAttribute <ValueAttribute>().Value;

            return(Math.Round(size / (double)FileSizeTypeModifier, 2));
        }
示例#6
0
        /// <summary>
        /// Creates a file of a specified size at a given location containing cryptographically randomized data.
        /// </summary>
        /// <param name="path">The location which the file should be created.</param>
        /// <param name="size">The size which the created size will be relative to the chosen <see cref="FileSizeType"/>.</param>
        /// <param name="type">The size type which determines how large the created file will be.</param>
        /// <returns>A flag determining whether the operation was successful.</returns>
        public bool GenerateFile(string path, int size, FileSizeType type)
        {
            try
            {
                if (!File.Exists(path))
                {
                    // Create our default buffer and block sizes.
                    int bufferSize = 1024 * 1024;
                    int blockSize  = (1024) / bufferSize;

                    // Check our size type and calculate the actual buffer and block length.
                    switch (type)
                    {
                    case FileSizeType.Kilobytes:
                        bufferSize = 1024;
                        blockSize  = (1024) / bufferSize;
                        break;

                    case FileSizeType.Megabytes:
                        blockSize = (1024 * 1024) / bufferSize;
                        break;

                    case FileSizeType.Gigabytes:
                        blockSize = (1024 * 1024 * 1024) / bufferSize;
                        break;

                    default:
                        return(false);
                    }

                    // Generate random data and write it to disk.
                    using (var file = LongFile.Open(path, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        byte[] data = new byte[bufferSize];
                        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                        for (int i = 0; i < size * blockSize; i++)
                        {
                            rng.GetBytes(data);
                            file.Write(data, 0, data.Length);
                        }
                        file.Flush();
                        file.Close();
                    }
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch { return(false); }
        }
        private long ConvertSizeToByte(long size, FileSizeType type)
        {
            switch (type)
            {
            case FileSizeType.Kb:
                size *= 1024;
                break;

            case FileSizeType.Mb:
                size *= (long)Math.Pow(1024, 2);
                break;

            case FileSizeType.Gb:
                size *= (long)Math.Pow(1024, 3);
                break;

            default:
                size *= (long)Math.Pow(1024, 4);
                break;
            }
            return(size);
        }
 public void RefreshFolder()
 {
     try
     {
         if ((Items != null))
         {
             Items.Clear();
         }
         if (SmallImageList != null)
         {
             SmallImageList.Images.Clear();
         }
         if (LargeImageList != null)
         {
             LargeImageList.Images.Clear();
         }
         if (this.DesignMode)
         {
             return;                 //System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime
         }
         //MessageBox.Show(this.DesignMode.ToString());
         if (System.IO.Directory.Exists(dir))
         {
             if (display == DisplayType.Directories || display == DisplayType.DirectoriesAndFiles)
             {
                 foreach (string fil in System.IO.Directory.GetDirectories(dir).OrderBy(f => f)) // Get Files In Folder
                 {
                     try
                     {
                         SmallImageList.Images.Add(fil, addImage(fil, dir));
                         LargeImageList.Images.Add(fil, addImage(fil, dir));
                         System.GC.Collect();
                         Int64 intTotal = 0;
                         foreach (var SizeFile in System.IO.Directory.GetFiles(fil, "*.*", System.IO.SearchOption.AllDirectories))
                         {
                             intTotal += (new FileInfo(SizeFile)).Length;
                         }
                         Items.Add(FormatFileName(fil), fil); // Add Files & File Properties To ListView
                         Items[Items.Count - 1].ImageKey   = fil;
                         Items[Items.Count - 1].ImageIndex = Items.Count - 1;
                         Items[Items.Count - 1].SubItems.Add((intTotal / FileSizeType.GetHashCode()) + " " + FileSizeType.ToString());
                         Items[Items.Count - 1].SubItems.Add("File Folder");
                         Items[Items.Count - 1].Tag = fil;
                         Items[Items.Count - 1].SubItems.Add(System.IO.Directory.GetLastWriteTime(fil).ToString());
                         intTotal = 0;
                     }
                     catch (Exception ex)
                     {
                         Console.WriteLine(ex.ToString());
                     }
                 }
             }
             if ((display == DisplayType.DirectoriesAndFiles || display == DisplayType.Files) && System.IO.Directory.Exists(dir))
             {
                 foreach (string fil in System.IO.Directory.GetFiles(dir, filt).OrderBy(f => f)) // Get Files In Folder
                 {
                     if (fil != null && !System.IO.Path.GetFileName(fil).StartsWith("(_)"))
                     {
                         SmallImageList.Images.Add(fil, addImage(fil, dir));
                         LargeImageList.Images.Add(fil, addImage(fil, dir));
                         System.GC.Collect();
                         Items.Add(FormatFileName(fil), fil);
                         Items[Items.Count - 1].SubItems.Add(((new FileInfo(fil)).Length / FileSizeType.GetHashCode()) + " " + FileSizeType.ToString());
                         Items[Items.Count - 1].Tag      = fil;
                         Items[Items.Count - 1].ImageKey = fil;
                         //Items[Items.Count - 1].ImageIndex = Items.Count - 1;
                         Items[Items.Count - 1].SubItems.Add(System.IO.File.GetLastWriteTime(fil).ToString());
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
         if (OnErrorGoToParentDirectory)
         {
             UpDirectory();
         }
     }
 }
示例#9
0
 public FileSize(long size, FileSizeType fileSizeType)
 {
     Initialize(size, fileSizeType);
 }