static void Main(string[] args)
        {
            Console.Title = "Random String Generator for .NET";

            StringGenerator generator = new StringGenerator();

            generator.MinLowerCaseChars = 2;
            generator.MinUpperCaseChars = 1;
            generator.MinNumericChars = 3;
            generator.MinSpecialChars = 2;
            generator.FillRest = CharType.LowerCase;

            Console.WriteLine(string.Format("Random string generated: {0}", generator.GenerateString(10)));

            Console.ReadLine();
        }
예제 #2
0
        public static bool GenerateBytes(int noOfFiles, int maxFileSize, out bool result, int sleep)
        {
            if (maxFileSize <= 0) //invalid max file size given
              throw new ArgumentException("File size should be greater that zero.", maxFileSize.ToString());

               byte[] firstBuffer; //first chunk of bytes
               byte[] secondBuffer; //second chunk of bytes
               var random = new Random(); //number generator
               StringGenerator generator = new StringGenerator();
               var fileNames = generator.NextStrings(noOfFiles); //generate random strings
               var interval = new TimeSpan(0, 0, 0, 0, sleep); //create seconds interval
               const int bytes = 1024; //bytes per KB

               try
               {
              for (var i = 0; i < noOfFiles; i++) //iterate over array elements
              {
                 try
                 {
                    var fi = new FileInfo(fileNames[i]); //create fileinfo object for the files in the array
                    var firstMaxBytesCount = random.Next(1, random.Next(1, (maxFileSize * bytes) / 2)); //random first count of bytes for read-write operation
                    var secondMaxBytesCount = (maxFileSize * bytes) - firstMaxBytesCount; //calculate remaining byte count for second read-write operation
                    var remaining = 0; //remaining length in bytes
                    var total = firstMaxBytesCount + secondMaxBytesCount; //total length in bytes
                    var length = 0; //length in bytes
                    var maxCount = 0; //max available count of bytes to write with this stream
                    var randomCount = 0; //random bytes count to write
                    firstBuffer = new byte[firstMaxBytesCount]; //init first buffer

                    using (var stream = fi.OpenWrite()) //create write only file stream
                    {
                       length = Convert.ToInt16(fi.Length);
                       maxCount = firstMaxBytesCount;
                       while (length < firstMaxBytesCount) //do stuff until file reaches first count of writable bytes
                       {
                          random.NextBytes(firstBuffer); //fill array of bytes with random numbers
                          randomCount = random.Next(1, maxCount); //generate random count of bytes
                          stream.Write(firstBuffer, 0, randomCount); //write bytes to the stream
                          length = Convert.ToInt16(stream.Length); //get new byte length
                          maxCount = firstMaxBytesCount - length; //calculate maximum count of bytes

                          if (!length.Equals(0))
                             Console.WriteLine("Writing {0} byte(s) to file {1}.file", randomCount, fileNames[i]);
                       }

                       remaining = total - length; //current remaining length in bytes
                    }

                    secondBuffer = new byte[secondMaxBytesCount]; //init second buffer
                    length = 0; //reset length for the second chunk

                    if (remaining == secondMaxBytesCount) //check that remaining fits into the next chunk
                    {
                       using (var fs = fi.Open(FileMode.Open, FileAccess.ReadWrite)) //open file stream to write new data
                       {
                          try
                          {
                             var fileLength = fs.Length; //current file length
                             var canSeek = fs.CanSeek;// seeking capability
                             var offSet = random.Next(0, Convert.ToInt16(fileLength)); //the point to write relative to the end of the first chunk written
                             var maxWritable = Convert.ToInt16(fileLength) - offSet; //max byte length to write to the stream with the seek operation

                             if (canSeek) //seek random position and update with random bytes
                             {
                                fs.Seek(offSet, SeekOrigin.Begin); //set current stream position
                                var randSize = random.Next(0, maxWritable); //generate random size
                                byte[] temp = new byte[randSize]; //init temp buffer
                                random.NextBytes(temp); //fill with bytes
                                fs.Write(temp, 0, randSize); //write the bytes
                                Console.WriteLine("Updating with {0} byte(s) file {1}.file", randSize, fileNames[i]);
                             }

                             //sleep before writing next chunk of bytes
                             if (!sleep.Equals(0))
                             {
                                Console.WriteLine("\nSleep for {0} milliseconds...", sleep);
                                Thread.Sleep(interval); //delay
                             }

                             maxCount = secondMaxBytesCount; //max available count of bytes to write with the second stream
                             while (length < secondMaxBytesCount)
                             {
                                random.NextBytes(secondBuffer);
                                randomCount = random.Next(1, maxCount);
                                fs.Write(secondBuffer, 0, randomCount);
                                length += randomCount;
                                maxCount = secondMaxBytesCount - length;

                                if (!length.Equals(0))
                                   Console.WriteLine("Writing {0} byte(s) to file {1}.file", randomCount, fileNames[i]);
                             }
                          }
                          catch (ArgumentException ae)
                          {
                             throw new ArgumentException("Seeking is attempted before the beginning of the stream.", ae.InnerException);
                          }
                          catch (FileNotFoundException fe)
                          {
                             throw new ArgumentException("File was not found: " + fileNames[i], fe.InnerException);
                          }
                       }
                    }
                 }
                 catch (ArgumentNullException ane)
                 {
                    throw new ArgumentException("File name was null : " + fileNames[i], ane.InnerException);
                 }
                 catch (ArgumentException ae)
                 {
                    throw new ArgumentException(ae.Message, ae.InnerException);
                 }
              }
               }
               catch (IndexOutOfRangeException ie)
               {
              throw new ArgumentException("Array index was out of range", ie.InnerException);
               }

               result = true;
               return result;
        }
 private void GenerateButton_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         if (ExpressionBox.Text.Trim() == "")
         {
             bool canParseMin = int.TryParse(MinLengthBox.Text.Trim(), out int minlength);
             bool canParseMax = int.TryParse(MaxLengthBox.Text.Trim(), out int maxlength);
             if (canParseMin && canParseMax)
             {
                 if (minlength > maxlength)
                 {
                     int temp = minlength;
                     minlength         = maxlength;
                     maxlength         = temp;
                     MinLengthBox.Text = minlength.ToString();
                     MaxLengthBox.Text = maxlength.ToString();
                 }
             }
             string Expression = "[";
             if (NumBox.IsChecked == true)
             {
                 Expression += "0-9,";
             }
             if (UpperBox.IsChecked == true)
             {
                 Expression += "A-Z,";
             }
             if (LowerBox.IsChecked == true)
             {
                 Expression += "a-z,";
             }
             Expression  = Expression.TrimEnd(',');
             Expression += "](";
             if (FixedButton.IsChecked == true)
             {
                 Expression += LengthBox.Text.Trim();
             }
             if (IndefiniteButton.IsChecked == true)
             {
                 Expression += MinLengthBox.Text.Trim() + "," + MaxLengthBox.Text.Trim();
             }
             Expression += ")";
             StringGenerator stringGenerator = new StringGenerator();
             string          result          = stringGenerator.Generator(Expression);
             MainRichTextBox.Document.Blocks.Clear();
             Paragraph p = new Paragraph();
             p.Inlines.Add(new Run(result));
             MainRichTextBox.Document.Blocks.Add(p);
         }
         else
         {
             StringGenerator stringGenerator = new StringGenerator();
             string          result          = stringGenerator.Generator(ExpressionBox.Text.Trim());
             MainRichTextBox.Document.Blocks.Clear();
             Paragraph p = new Paragraph();
             p.Inlines.Add(new Run(result));
             MainRichTextBox.Document.Blocks.Add(p);
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "错误");
     }
 }