1

So I'm trying to fill a media device in increments and measure the speed, and it works until I go to write data from 75% to 95% (We don't completely fill it)

When I go to set the file length (Ex: I'm trying to make a 3.1GB file when the disk has 3.72 GB available I get the following exception

"The parameter is incorrect."

StackTrace

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.SetLengthCore(Int64 value)
at System.IO.FileStream.SetLength(Int64 value)
at DiskSpeed.Write.WriteFileFromMemory(Byte[] buffer, String path, Int64 fileSize, Int32 currentIteration, Int32 totalIterations, CancellationToken ct)

Here's the code

using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None, 8, FileOptions.WriteThrough | FileFlagNoBuffering))
{
    fs.SetLength(fileSize);
    .......
}

I'm using this to write chunks to a removalable media in increments of 25%, but after 75%, I only write a 20% chunk. Going from 0-75% works fine, it's the final chunk that throws this error. Yes, I have double checked the file size, THERE IS SPACE!!!

Does anyone have any ideas? I'm at my wits end and I can't seem to find anything online.

Calvin
  • 710
  • 1
  • 10
  • 28
  • According to the [documentation](https://msdn.microsoft.com/en-us/library/system.io.filestream.setlength%28v=vs.110%29.aspx) *A stream must support both writing and seeking for SetLength to work.*, but I don' see anything about seeking in your example... Not sure if that might help. – default Mar 24 '15 at 13:51
  • 1
    What is the exact type of the exception? – Marcin Hoppe Mar 24 '15 at 13:51
  • 2
    what is `fileSize` when you're calling `SetLength`? – default Mar 24 '15 at 13:52
  • Marcin, System.IO exception, Default, it's not hard coded, I updated the question with details. It should be whatever value would take the disk from 75% filled to 95% filled, I use the DiskInfo class to calculate this, and have checked the values. CanSeek and CanWrite are both true. – Calvin Mar 24 '15 at 13:56
  • What the heck is FileFlagNoBuffering? Also why such small buffer of 8 bytes? – Dusan Mar 24 '15 at 14:01
  • The stack trace suggests the exception is thrown during seeking. I'd use something like Process Monitor or DiskMon from Sysinternals to check what I/O operation triggers an exception. – Marcin Hoppe Mar 24 '15 at 14:04
  • So you wrote 75% of the file and then you set the stream size to 75% of the on disk file size? Also note that, File stream throws only IOException, NotSupportedException, and ArgumentOutOfRangeException, where as “The parameter is incorrect” sounds like an argument exception and hence not related the file stream – Ahmad Mar 24 '15 at 14:13
  • Ahmad, I filled the disk to 75%, and then I calculate how many bytes it would be to fill the disk to 90%, but when I set the length of that file I get the exception. The length is correct, I have double and triple checked that code, the values are correct!!!!!!!!! – Calvin Mar 24 '15 at 14:14
  • 1
    Take a look at the: http://stackoverflow.com/a/1374156/461810 It can be problematic, maybe this flag is causing problem – Dusan Mar 24 '15 at 14:15
  • Thanks Dusan, I've had to switch focus for a moment on a different project but I will try that in a bit! – Calvin Mar 24 '15 at 14:27
  • 3
    When you use the FileFlagNoBuffering option (regardless of misspelling) then it is your job to ensure that you seek to a position that's an integer multiple of the volume sector size. "95%" is not a great measure for that. – Hans Passant Mar 24 '15 at 14:34
  • Yep, I'm going to chalk this one up to me not understanding exactly what the flag does. I've removed it from now and will read up on it. I'm not all that educated on the inner workings of media. – Calvin Mar 24 '15 at 15:00

1 Answers1

1
private const FileOptions FileFlagNoBuffering = (FileOptions)0x20000000;

This was causing the issue. Thanks Dusan!

Calvin
  • 710
  • 1
  • 10
  • 28