Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4622162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:48:17+00:00 2026-05-22T02:48:17+00:00

I’m trying to determine worst case disk speed, so I wrote the following function.

  • 0

I’m trying to determine worst case disk speed, so I wrote the following function.

static public decimal MBytesPerSec(string volume)
{
    string filename = volume + "\\writetest.tmp";

    if (System.IO.File.Exists(filename))
        System.IO.File.Delete(filename);

    System.IO.StreamWriter file = new System.IO.StreamWriter(filename);

    char[] data = new char[64000];
    Stopwatch watch = new Stopwatch();
    watch.Start();

    int i = 0;

    for (; i < 1000; i++)
    {
        file.Write(data);
        if (watch.ElapsedMilliseconds > 2000)
        {
            break;
        }
    }

    watch.Stop();
    file.Close();

    System.IO.File.Delete(volume + "\\test.txt");
    decimal mbytessec = (i * 64 / watch.ElapsedMilliseconds);
    return mbytessec;
}

The function works OK, but the writes are getting cached, so the speed is not worst case.

In WIN32 C++, I would simply create the file with FILE_FLAG_NO_BUFFERING, FILE_FLAG_WRITE_THROUGH options, and then make sure to follow the non-cached writing rules (write to the file at sector size offsets, with minimum of 4k writes)

I found one article that discusses the .NET technique.

So I wrote a new function (ignore the math errors).

static public decimal MBytesPerSecNonCached(string volume)
{
    const FileOptions FILE_FLAG_NO_BUFFERING = (FileOptions)0x20000000;

    string filename = volume + "\\writetest.tmp";

    using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1024, FileOptions.WriteThrough | FILE_FLAG_NO_BUFFERING))
    {
        byte[] data = new byte[65535];
        int i = 0;

        Stopwatch watch = new Stopwatch();
        watch.Start();

        for (; i < 1000; i++)
        {
            fs.Write(data, 0, 65535);
            if (watch.ElapsedMilliseconds > 2000)
            {
                break;
            }
        }

        watch.Stop();
        fs.Close();

        System.IO.File.Delete(filename);

        decimal mbytessec = (i * 64 / watch.ElapsedMilliseconds);

        return mbytessec;
    }
}

This function works for 4k, 16K and 32K write sizes, but once I try 64K write sizes, I get an exception:

IO operation will not work. Most likely the file will become too long or the handle was not opened to support synchronous IO operations.

So, how can I fix this so I can test with larger than 32KB write sizes (64KB to 4096KB)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:48:17+00:00Added an answer on May 22, 2026 at 2:48 am

    Try some unmanaged code:

    [DllImport("kernel32", SetLastError = true)]
            static extern unsafe SafeFileHandle CreateFile(
                string FileName,           // file name
                uint DesiredAccess,        // access mode
                uint ShareMode,            // share mode
                IntPtr SecurityAttributes, // Security Attr
                uint CreationDisposition,  // how to create
                uint FlagsAndAttributes,   // file attributes
                IntPtr hTemplate // template file  
                );
    const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
    
    SafeFileHandle handle = CreateFile("filename",
                                (uint)FileAccess.Write,
                                (uint)FileShare.None,
                                IntPtr.Zero,
                                (uint)FileMode.Open,
                                 FILE_FLAG_NO_BUFFERING,
                                IntPtr.Zero);
    
    var unBufferedStream = new FileStream(handle,FileAccess.Read,blockSize,false);
    

    now you should have access to an unbuffered stream which you can read and write however you please with no constraints

    For the record….you can also disable caching like this:

    [DllImport("KERNEL32", SetLastError = true)]
            public extern static int DeviceIoControl(IntPtr hDevice, uint IoControlCode,
                IntPtr lpInBuffer, uint InBufferSize,
                IntPtr lpOutBuffer, uint nOutBufferSize,
                ref uint lpBytesReturned,
                IntPtr lpOverlapped);
            [DllImport("KERNEL32", SetLastError = true)]
            public extern static int CloseHandle(
            IntPtr hObject);
    
    [StructLayout(LayoutKind.Sequential)]
            public struct DISK_CACHE_INFORMATION
            {            
                public byte ParametersSavable;            
                public byte ReadCacheEnabled;            
                public byte WriteCacheEnabled;
                public int ReadRetentionPriority;//DISK_CACHE_RETENTION_PRIORITY = enum = int
                public int WriteRetentionPriority;//DISK_CACHE_RETENTION_PRIORITY = enum = int
                public Int16 DisablePrefetchTransferLength;//WORD            
                public byte PrefetchScalar;            
            }
    
    public void SetDiskCache(byte val)
            {
                IntPtr h = CreateFile("\\\\.\\PHYSICALDRIVE0", (uint)FileAccess.Read | (uint)FileAccess.Write, (uint)FileShare.Write, IntPtr.Zero, (uint)FileMode.Open, 0, IntPtr.Zero);
                DISK_CACHE_INFORMATION sInfo = new DISK_CACHE_INFORMATION();
                IntPtr ptrout = Marshal.AllocHGlobal(Marshal.SizeOf(sInfo));
                Marshal.StructureToPtr(sInfo, ptrout, true);            
                uint dwWritten = 0;
                int ret = DeviceIoControl(h,IOCTL_DISK_GET_CACHE_INFORMATION,IntPtr.Zero,0,ptrout,(uint)Marshal.SizeOf(sInfo),ref dwWritten,IntPtr.Zero);            
                sInfo = (DISK_CACHE_INFORMATION)Marshal.PtrToStructure(ptrout,typeof(DISK_CACHE_INFORMATION));            
                sInfo.ReadCacheEnabled = val;
                // acuma trimite structura modificata
                IntPtr ptrin = Marshal.AllocHGlobal(Marshal.SizeOf(sInfo));
                Marshal.StructureToPtr(sInfo, ptrin, true);            
                ret = DeviceIoControl(h, IOCTL_DISK_SET_CACHE_INFORMATION, ptrin, (uint)Marshal.SizeOf(sInfo), IntPtr.Zero, 0, ref dwWritten, IntPtr.Zero);            
                CloseHandle(h);            
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.