Creating a Random File in C#

前端 未结 5 1918
执念已碎
执念已碎 2020-12-14 07:29

I am creating a file of a specified size - I don\'t care what data is in it, although random would be nice. Currently I am doing this:

        var sizeInMB          


        
5条回答
  •  不思量自难忘°
    2020-12-14 07:56

    You can use this following class created by me for generate random strings

    using System;
    using System.Text;
    
    public class RandomStringGenerator
    {
        readonly Random random;
    
        public RandomStringGenerator()
        {
            random = new Random();
        }
        public string Generate(int length)
        {
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            var stringBuilder = new StringBuilder();
    
            for (int i = 0; i < length; i++)
            {
                char ch = (char)random.Next(0,255 );
                stringBuilder.Append(ch);
            }
    
            return stringBuilder.ToString();
    
        }
    
    }
    

    for using

     int length = 10;
            string randomString = randomStringGenerator.Generate(length);
    

提交回复
热议问题