Click here to Skip to main content
12,759,979 members (30,499 online)
Click here to Skip to main content

Stats

75.4K views
1.8K downloads
55 bookmarked
Posted 23 Jan 2005

Manipulate Alternate Data Streams

, 23 Jan 2005
A library you can use to encode "hidden" data in existing files.
ADSTester
ADS.dll
ADSTester.csproj.user
App.ico
ADS
AlternateDataStreams.csproj.user
App.ico
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ADSTester {
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label lblFile;
		private System.Windows.Forms.TextBox txtFile;
		private System.Windows.Forms.Button cmdBrowse;
		private System.Windows.Forms.TextBox txtStream;
		private System.Windows.Forms.Label lblStream;
		private System.Windows.Forms.Label lblData;
		private System.Windows.Forms.TextBox txtData;
		private System.Windows.Forms.Button cmdLoad;
		private System.Windows.Forms.Button cmdClose;
		private System.Windows.Forms.Button cmdSave;
      private System.Windows.Forms.Button cmdClear;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMain() {
			InitializeComponent();
		}

		/// <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.lblFile = new System.Windows.Forms.Label();
         this.txtFile = new System.Windows.Forms.TextBox();
         this.cmdBrowse = new System.Windows.Forms.Button();
         this.lblStream = new System.Windows.Forms.Label();
         this.txtStream = new System.Windows.Forms.TextBox();
         this.txtData = new System.Windows.Forms.TextBox();
         this.lblData = new System.Windows.Forms.Label();
         this.cmdLoad = new System.Windows.Forms.Button();
         this.cmdSave = new System.Windows.Forms.Button();
         this.cmdClose = new System.Windows.Forms.Button();
         this.cmdClear = new System.Windows.Forms.Button();
         this.SuspendLayout();
         // 
         // lblFile
         // 
         this.lblFile.AutoSize = true;
         this.lblFile.Location = new System.Drawing.Point(8, 8);
         this.lblFile.Name = "lblFile";
         this.lblFile.Size = new System.Drawing.Size(26, 16);
         this.lblFile.TabIndex = 0;
         this.lblFile.Text = "File:";
         // 
         // txtFile
         // 
         this.txtFile.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtFile.Location = new System.Drawing.Point(64, 6);
         this.txtFile.Name = "txtFile";
         this.txtFile.ReadOnly = true;
         this.txtFile.Size = new System.Drawing.Size(560, 20);
         this.txtFile.TabIndex = 1;
         this.txtFile.Text = "";
         // 
         // cmdBrowse
         // 
         this.cmdBrowse.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
         this.cmdBrowse.Location = new System.Drawing.Point(624, 6);
         this.cmdBrowse.Name = "cmdBrowse";
         this.cmdBrowse.Size = new System.Drawing.Size(20, 20);
         this.cmdBrowse.TabIndex = 2;
         this.cmdBrowse.Text = "...";
         this.cmdBrowse.Click += new System.EventHandler(this.cmdBrowse_Click);
         // 
         // lblStream
         // 
         this.lblStream.AutoSize = true;
         this.lblStream.Location = new System.Drawing.Point(8, 28);
         this.lblStream.Name = "lblStream";
         this.lblStream.Size = new System.Drawing.Size(44, 16);
         this.lblStream.TabIndex = 3;
         this.lblStream.Text = "Stream:";
         // 
         // txtStream
         // 
         this.txtStream.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtStream.Location = new System.Drawing.Point(64, 26);
         this.txtStream.Name = "txtStream";
         this.txtStream.Size = new System.Drawing.Size(560, 20);
         this.txtStream.TabIndex = 4;
         this.txtStream.Text = "";
         // 
         // txtData
         // 
         this.txtData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
         this.txtData.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.txtData.Location = new System.Drawing.Point(64, 56);
         this.txtData.Multiline = true;
         this.txtData.Name = "txtData";
         this.txtData.Size = new System.Drawing.Size(560, 168);
         this.txtData.TabIndex = 5;
         this.txtData.Text = "";
         // 
         // lblData
         // 
         this.lblData.AutoSize = true;
         this.lblData.Location = new System.Drawing.Point(8, 56);
         this.lblData.Name = "lblData";
         this.lblData.Size = new System.Drawing.Size(31, 16);
         this.lblData.TabIndex = 6;
         this.lblData.Text = "Data:";
         // 
         // cmdLoad
         // 
         this.cmdLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.cmdLoad.Location = new System.Drawing.Point(64, 232);
         this.cmdLoad.Name = "cmdLoad";
         this.cmdLoad.TabIndex = 7;
         this.cmdLoad.Text = "Load";
         this.cmdLoad.Click += new System.EventHandler(this.cmdLoad_Click);
         // 
         // cmdSave
         // 
         this.cmdSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.cmdSave.Location = new System.Drawing.Point(144, 232);
         this.cmdSave.Name = "cmdSave";
         this.cmdSave.TabIndex = 8;
         this.cmdSave.Text = "Save";
         this.cmdSave.Click += new System.EventHandler(this.cmdSave_Click);
         // 
         // cmdClose
         // 
         this.cmdClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
         this.cmdClose.Location = new System.Drawing.Point(552, 232);
         this.cmdClose.Name = "cmdClose";
         this.cmdClose.TabIndex = 9;
         this.cmdClose.Text = "Close";
         this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click);
         // 
         // cmdClear
         // 
         this.cmdClear.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
         this.cmdClear.Location = new System.Drawing.Point(224, 232);
         this.cmdClear.Name = "cmdClear";
         this.cmdClear.TabIndex = 10;
         this.cmdClear.Text = "Clear";
         this.cmdClear.Click += new System.EventHandler(this.cmdClear_Click);
         // 
         // frmMain
         // 
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(648, 273);
         this.Controls.Add(this.cmdClear);
         this.Controls.Add(this.cmdClose);
         this.Controls.Add(this.cmdSave);
         this.Controls.Add(this.cmdLoad);
         this.Controls.Add(this.lblData);
         this.Controls.Add(this.txtData);
         this.Controls.Add(this.txtStream);
         this.Controls.Add(this.lblStream);
         this.Controls.Add(this.cmdBrowse);
         this.Controls.Add(this.txtFile);
         this.Controls.Add(this.lblFile);
         this.MinimumSize = new System.Drawing.Size(424, 168);
         this.Name = "frmMain";
         this.Text = "ADS Tester";
         this.ResumeLayout(false);

      }
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {
			Application.Run(new frmMain());
		}

		private void cmdBrowse_Click(object sender, System.EventArgs e) {
			using (System.Windows.Forms.OpenFileDialog ofd = new OpenFileDialog()) {
				ofd.Title = "Open file for stream operation";
				ofd.Filter = "All files (*.*)|*.*";
				ofd.Multiselect	= false;
				
				System.Windows.Forms.DialogResult dr = ofd.ShowDialog(this);
				
				if (dr == System.Windows.Forms.DialogResult.OK)
					txtFile.Text = ofd.FileName;
			}
		}

		private void cmdLoad_Click(object sender, System.EventArgs e) {
			if (txtFile.Text.Length > 0 && txtStream.Text.Length > 0) {
            string   sText    = string.Empty;

            try {
               sText    = AlternateDataStreams.ADSFile.Read(txtFile.Text, txtStream.Text);

               MessageBox.Show(this, 
                              "Stream data successfully loaded.", 
                              "Stream data loaded", 
                               MessageBoxButtons.OK,
                               MessageBoxIcon.Information);
            } catch (AlternateDataStreams.StreamNotFoundException adse) {
               MessageBox.Show(this, 
                              "Stream " + adse.Stream + " not found in " + adse.FileName, 
                              "Stream not found",
                               MessageBoxButtons.OK, 
                               MessageBoxIcon.Error);
            } catch (Exception ge) {
               MessageBox.Show(this, 
                              "The following error occured:\n\n" + ge.Message, 
                              "Generic error",
                               MessageBoxButtons.OK, 
                               MessageBoxIcon.Error);
            } finally {
               txtData.Text = sText;
            }
			}
		}

      private void cmdClose_Click(object sender, System.EventArgs e) {
         System.Environment.Exit(0);
      }

      private void cmdSave_Click(object sender, System.EventArgs e) {
         if (txtFile.Text.Length > 0 && txtStream.Text.Length > 0) {
            uint  length = AlternateDataStreams.ADSFile.Write(txtData.Text, 
                                                              txtFile.Text, 
                                                              txtStream.Text);

            MessageBox.Show(this, 
                            length.ToString() + " bytes written to the stream.", 
                           "Stream written",
                            MessageBoxButtons.OK, 
                            MessageBoxIcon.Information);
         }
      }

      private void cmdClear_Click(object sender, System.EventArgs e) {
         txtStream.Text = string.Empty;
         txtData.Text   = string.Empty;
      }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Sean Michael Murphy
Product Manager
Canada Canada
I'm a graduate of the University of Toronto with a degree in zoology. I'm currently a software development manager with a large Canadian financial institution, and a passionate squash player.

I am a proud daddy to Alex and Sarah.

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Jan 2005
Article Copyright 2005 by Sean Michael Murphy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid