Is there a way to disable the write behind caching policy on a disk drive programmatically using C# in windows?

2.1k Views Asked by At

I need to remove write behind caching on the disk drives of our servers. Doing so in windows ==> Device Manager ==> Disk Drives ==> (right click) Properties ==> Policies ==> (CheckBox) Enable write behind caching | is easy but windows re-enables it automatically. I believe this happens with updates.

I want to know if I can access these policies using C# to disable them on a daily basis.

Anyone know of a way?

I have looked at DriveInfo but found no methods to access the policies.

2

There are 2 best solutions below

0
Blixem On BEST ANSWER

Okay, So I found a solution for this issue.

As OlivierRogier also mentioned. This setting seems to be stored in the Registry under:

HKLM\SYSTEM\CurrentControlSet\Enum\(IDE OR SCSI)\Diskxxxxxxxxxxxx\DeviceParameters\Disk Key : UserWriteCacheSetting where xxxxxxx is manufacturer information.

Link HERE

I set up some code that checks this setting on a daily basis => changes it => restarts the computer. Will share the code if requested.

Thank you everyone for your input. Blixem

3
Christopher On

I got 2 possible solutions, which both aim at solving the issue on the per-file level rather then OS settings level.

If you wanted to change a Policy for Policies sake, then the Windows Policy Management maay have an option for this.

WriteThrough

Now there is a whole host of caches in play here - Application/Class, OS, the RAM in the Disk Eletronics. And I am unsure if it can affect the specific cache(s) you are worried about. But FileStream has a option called "WriteThrough":

Indicates that the system should write through any intermediate cache and go directly to disk.

https://learn.microsoft.com/en-us/dotnet/api/system.io.fileoptions

The Application/Class cache is trivial and to 99% disabeled by this.

The mention of "System" indicates to me that it will get to the OS cache (wich you seem worried about). But this is only a single line in the Documentation, so I can not say with certain it goes down to OS Level. But if there is one Option I would define for logging to a file and asume to be enabeled on all existing loggers, it this is it.

Asuming this is not for logging or similar streaming work and the core issue is the danger of corrupting file with partial writes, a minor redesign can fix that issue.

Writing to Tempfile -> Move

One surefire way to avoid corrupting a file when (re)writing it, is to write to a different temporary file - and then just swap those files out with a move (wich ideally will only change the File System Name entries). I did write some example code for this not to long ago after seeing this behavior in anything down to Word Processors:

string sourcepath; //containts the source file path, set by other code
string temppath; //containts the path of the tempfile. Should be in the same folder, and thus same partiion

//Open both Streams, can use a single using for this
//The supression of any Buffering on the output should be optional and will be detrimental to performance
using(var sourceStream = File.OpenRead(sourcepath), 
  outStream = File.Create(temppath, 0, FileOptions.WriteThrough )){

    string line = "";

    //itterte over the input
    while((line = streamReader.ReadLine()) != null){
        //do processing on line here

        outStream.Write(line);
    }
}

//replace the files. Pretty sure it will just overwrite without asking
File.Move(temppath, sourcepath);

The downside is of course you will temporarily need twice the Disk Space and it will cause a lot of extra write work on some modifications.