I've been incredibly busy at work lately and I really will get back to this blog soon. Anyway, came across this little bit of code whilst combing through an old backup, it basically lets you make a file of any size with any name (file contains totally random, non-compressible data) - I used it for testing uploading functions - oh, it's a winforms app before I forget :-):

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.IO;

using System.Security.Cryptography;

 

namespace DummyFileMaker

{

    /// <summary>

    /// Summary description for Form1.

    /// </summary>

    public class Form1 : System.Windows.Forms.Form

    {

        private System.Windows.Forms.TextBox FileSize;

        private System.Windows.Forms.Button MakeFileButton;

        private System.Windows.Forms.TextBox FilePath;

        private System.Windows.Forms.Label label1;

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.Container components = null;

 

        public Form1()

        {

            //

            // Required for Windows Form Designer support

            //

            InitializeComponent();

 

            //

            // TODO: Add any constructor code after InitializeComponent call

            //

        }

 

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose( bool disposing )

        {

            if( disposing )

            {

                if (components != null)

                {

                    components.Dispose();

                }

            }

            base.Dispose( disposing );

        }

 

        #region Windows Form Designer generated code

        /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.FileSize = new System.Windows.Forms.TextBox();

            this.MakeFileButton = new System.Windows.Forms.Button();

            this.FilePath = new System.Windows.Forms.TextBox();

            this.label1 = new System.Windows.Forms.Label();

            this.SuspendLayout();

            //

            // FileSize

            //

            this.FileSize.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

                | System.Windows.Forms.AnchorStyles.Left)));

            this.FileSize.Location = new System.Drawing.Point(16, 8);

            this.FileSize.Name = "FileSize";

            this.FileSize.TabIndex = 0;

            this.FileSize.Text = "10";

            //

            // MakeFileButton

            //

            this.MakeFileButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)

                | System.Windows.Forms.AnchorStyles.Right)));

            this.MakeFileButton.Location = new System.Drawing.Point(160, 40);

            this.MakeFileButton.Name = "MakeFileButton";

            this.MakeFileButton.TabIndex = 1;

            this.MakeFileButton.Text = "make file";

            this.MakeFileButton.Click += new System.EventHandler(this.MakeFileButton_Click);

            //

            // FilePath

            //

            this.FilePath.Location = new System.Drawing.Point(16, 40);

            this.FilePath.Name = "FilePath";

            this.FilePath.TabIndex = 2;

            this.FilePath.Text = "c:\\dummyfile.gif";

            //

            // label1

            //

            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));

            this.label1.Location = new System.Drawing.Point(144, 8);

            this.label1.Name = "label1";

            this.label1.Size = new System.Drawing.Size(128, 23);

            this.label1.TabIndex = 3;

            //

            // Form1

            //

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(292, 69);

            this.Controls.Add(this.label1);

            this.Controls.Add(this.FilePath);

            this.Controls.Add(this.MakeFileButton);

            this.Controls.Add(this.FileSize);

            this.MaximumSize = new System.Drawing.Size(300, 96);

            this.MinimumSize = new System.Drawing.Size(300, 96);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);

 

        }

        #endregion

 

        /// <summary>

        /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.Run(new Form1());

        }

 

        private void MakeFileButton_Click(object sender, System.EventArgs e)

        {

            label1.Text = "Creating file: " + FilePath.Text;

            using(FileStream fle = new FileStream(FilePath.Text,FileMode.Create,FileAccess.Write,FileShare.None,4096,false))

            {

                double numBytes = Convert.ToDouble(FileSize.Text) * (1024 * 1024);

                byte[] bytArr = new byte[(int)numBytes];

                RNGCryptoServiceProvider rng = new  RNGCryptoServiceProvider();

                rng.GetBytes(bytArr);

                fle.Write(bytArr,0,bytArr.Length);

                fle.Close();

            }

            label1.Text = "File Created";

        }

    }

}

posted on Monday, November 08, 2004 5:48 PM | Print

Comments

No comments posted yet.
Post Comment
Title *  
Name *  
Email
Url
Comment *  
Please add 4 and 7 and type the answer here: