title: Generate 1 MB (or n MB) text file in C#
body: <p>I need to create a text file (alphanumeric) with size (exact or nearly) in MB equal to my input number, such as 1 MB. I tried to generated a string, with think that one char is 16 bit or 2 byte, so:
  1KB = 1024 Bytes = 1024 x 8 Bits = 1024 x 8 / 16 chars = 512 chars.
  But the generated file is not very seem to be right :(
  If I generate byte array and write it to file, the size is pretty correct!</p>
  
  <pre><code> public static void generateDummyFileBySize(long sizeInMb, string filePath) {
  
              byte[] data = new byte[sizeInMb * 1024 * 1024];
              Random rng = new Random();
              rng.NextBytes(data);
              File.WriteAllBytes(filePath, data);
          }
  </code></pre>
  
  <p>Help me please. This is the code I used to generated text file</p>
  
  <pre><code>public static String generateStringSize(long sizeByte)
          {
  
              StringBuilder sb = new StringBuilder();
              Random rd = new Random();
              /**
               * Byte -&gt; Bit -&gt; Divide by 16 to num of char
               * */
              var numOfChars = sizeByte * 8 ;
              string allows = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
              int maxIndex = allows.Length - 1;
              for (int i = 0; i &lt; numOfChars; i++)
              {
                  int index = rd.Next(maxIndex);
                  char c = allows[index];
                  sb.Append(c);
              }
              return sb.ToString();
          }
  
  public static void generateTextFileBySize(long size, string fileName)
          {
              long sizeOld = size;
              try
              {
  
                  String s;
                  String path = Directory.GetCurrentDirectory();
                  var physicPath = path + Path.DirectorySeparatorChar + fileName;
                  File.Delete(physicPath);
                  if (size &lt;= MAX_SIZE)
                  {
                      s = StringUtil.generateStringSize(size);
                      Console.WriteLine("Generated a string with length " + size);
                      File.WriteAllText(physicPath, s, Encoding.UTF8);
                  }
                  else
                  {
                      while (size &gt; MAX_SIZE)
                      {
                          s = StringUtil.generateStringSize(MAX_SIZE);
                          Console.WriteLine("Appending " + MAX_SIZE + " to file");
                          File.AppendAllText(physicPath, s, Encoding.UTF8);
                          size -= MAX_SIZE;
                      }
                      s = StringUtil.generateStringSize(size);
                      File.AppendAllText(physicPath, s, Encoding.UTF8);
                  }
                  Console.WriteLine("Created file named " + fileName + " with size " + sizeOld + " bytes.");
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Error when generating file");
                  Console.WriteLine(ex.Message);
              }
          }
  </code></pre>