Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>This is because you have created a byte array of size 1024. So, even in case inputStream length is less than 1024, it will still write 1024 bytes to your file.</p> <p>You should initialize it with inputStream size so that only limited bytes gets written to your file:</p> <pre><code>byte[] buffer = new byte[inputStream.Length]; </code></pre> <p></strike></p> <p><strong>UPDATE</strong></p> <p>Silvermind in comment pointed out right, let buffer array size to be 1024. Instead <code>use byte size returned by Read</code> method to write to your file so that only bytes read gets written to file:</p> <pre><code>int bytesRead; while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) &gt; 0) { Console.WriteLine("bufferlength " + buffer.Where(x =&gt; x != 0) .ToArray().Length); Console.WriteLine(new FileInfo(@"D:\Temp\test2.txt").Length); outputStream.Write(buffer, 0, bytesRead); &lt;- HERE outputStream.Flush(); } </code></pre> <hr> <p>On a sidenote, you should <code>dispose FileStream's</code> after use to avoid any memory leaks. You can <code>wrap them in using block</code> which will dispose it automatically for you after use.</p> <pre><code>using (FileStream inputStream = new FileStream(@"C:\Temp\test.txt", FileMode.Open)) { using (FileStream outputStream = new FileStream(@"C:\Temp\test2.txt", FileMode.OpenOrCreate)) { byte[] buffer = new byte[inputStream.Length]; Console.WriteLine(new FileInfo(@"C:\Temp\test.txt").Length + "\n"); int bytesRead; while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) &gt; 0) { Console.WriteLine("bufferlength " + buffer.Where(x =&gt; x != 0) .ToArray().Length); Console.WriteLine(new FileInfo(@"D:\Temp\test2.txt").Length); outputStream.Write(buffer, 0, bytesRead); &lt;- HERE outputStream.Flush(); } Console.WriteLine("\n" + inputStream.Length); Console.WriteLine(outputStream.Length); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload