Recently I needed to do some testing in Exchange 2010 OWA using different sized files. Rather than e-mail some file I found on my system which happened to be “close enough” to the right file size, I decieded to write a Powershell type file which I could use to create these files.
I wanted to make sure I could create some dynamicly sized files so I created a type called TestFiles.
Here is the code I came up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
Add-Type -TypeDefinition @" using System; public class TestFiles { private string OneKB = new string('X', 1024); private string OneMB = new string('X', (1024 * 1024)); public void CreateFileWithSizeInMB(int size, string FilePath) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, true)) { for (int i = 0; i < size; i++) { file.Write(OneMB); } } } public void CreateFileWithSizeInKB(int size, string FilePath) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, true)) { for (int i = 0; i < size; i++) { file.Write(OneKB); } } } } "@ |
The nice thing about this is if I want to create some other kind of test file, I can just add the code for it and all my test files can be found in one place. :d
Here is an example usage:
1 2 |
$tf = new-object TestFiles $tf.CreateFileWithSizeInMB(12,"c:\users\glaisne\desktop\12MB.txt") |
Leave a Reply