Creating a Random File in C#

Well, a very simple solution: byte[] data = new byte[sizeInMb * 1024 * 1024]; Random rng = new Random(); rng.NextBytes(data); File.WriteAllBytes(fileName, data); A slightly more memory efficient version 🙂 // Note: block size must be a factor of 1MB to avoid rounding errors 🙂 const int blockSize = 1024 * 8; const int blocksPerMb = … Read more

Difference between OPEN_ALWAYS and CREATE_ALWAYS in CreateFile() of Windows API

CREATE_ALWAYS also truncates the contents if the file already exists. On the other hand, OPEN_ALWAYS will not clobber an already existing file. Here’s how the different values work in tabular form: | When the file… This argument: | Exists Does not exist ————————-+—————————————————— CREATE_ALWAYS | Truncates Creates CREATE_NEW +———–+ Fails Creates OPEN_ALWAYS ===| does this … Read more

Create a file if it doesn’t exist

For Linux users. If you don’t need atomicity you can use os module: import os if not os.path.exists(‘/tmp/test’): os.mknod(‘/tmp/test’) macOS and Windows users. On macOS for using os.mknod() you need root permissions. On Windows there is no os.mknod() method. So as an alternative, you may use open() instead of os.mknod() import os if not os.path.exists(‘/tmp/test’): … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)