Project File Encoding Conversion

From Frederick Chapleau Wiki

Jump to: navigation, search

Contents

Great, but... why?

File encoding is one of the most obscure encoding that I saw, and you only know what is it, when you encounter a problem. When developping in a heterogous environment, it is mostly the case that this will happen a day, or the other.

This utility is for one shot conversion, mainly in ETL processes, or can be the base of a conversion tool included in an ETL process.

License

Complete sources are released under the MIT License, and are available here.

Download

You can download it here

Source

Code

    public partial class Master : Form
    {
        public Master()
        {
            InitializeComponent();
        }
 
 
        private void panelDrop_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Link;
        }
 
        private void panelDrop_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData("FileDrop");
            foreach (string sourceFile in files)
            {   
                FileInfo fi = new FileInfo(sourceFile);
                string targetFile = fi.DirectoryName + "\\" + Path.GetFileNameWithoutExtension(sourceFile) + "." + cboEncodings.Text + fi.Extension;
 
                if (!chkAutoName.Checked)
                {
                    targetFile = string.Empty;
                    DialogResult r = saveFileDlg.ShowDialog();
                    if (r == DialogResult.OK)
                    {
                        targetFile = saveFileDlg.FileName;
                    }
                }
 
                if (targetFile != string.Empty)
                {
                    try
                    {
                        Encoding targetEncoding = Encoding.GetEncoding(cboEncodings.Text);
 
                        StreamWriter wr = new StreamWriter(targetFile, false, targetEncoding);
 
                        StreamReader sr = new StreamReader(sourceFile, true);
                        tsStatus.Text = "From " + sr.CurrentEncoding.EncodingName;
                        string line = sr.ReadLine();
                        while (line != null)
                        {
                            wr.WriteLine(
                                targetEncoding.GetString(
                                    System.Text.Encoding.Convert(
                                        Encoding.Unicode,
                                        targetEncoding,
                                        Encoding.Unicode.GetBytes(line))));
                            line = sr.ReadLine();
                        }
                        sr.Close();
                        wr.Close();
 
                        tsOutputFile.Text = Path.GetFileName(targetFile) + " Done.";
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
        }
 
        private void Master_Load(object sender, EventArgs e)
        {
            EncodingInfo[] encodings = Encoding.GetEncodings();
            foreach (EncodingInfo ei in encodings)
            {
                int index = cboEncodings.Items.Add(ei.Name);
                if(ei.Name == "utf-8")
                    cboEncodings.SelectedIndex = index;
            }
        }
 
    }

Designer Code

    partial class Master
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Master));
            this.saveFileDlg = new System.Windows.Forms.SaveFileDialog();
            this.cboEncodings = new System.Windows.Forms.ComboBox();
            this.panelDrop = new System.Windows.Forms.Panel();
            this.lblDragHere = new System.Windows.Forms.Label();
            this.lblTargetEncoding = new System.Windows.Forms.Label();
            this.chkAutoName = new System.Windows.Forms.CheckBox();
            this.statusStrip1 = new System.Windows.Forms.StatusStrip();
            this.tsStatus = new System.Windows.Forms.ToolStripStatusLabel();
            this.tsOutputFile = new System.Windows.Forms.ToolStripStatusLabel();
            this.panelDrop.SuspendLayout();
            this.statusStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // cboEncodings
            // 
            this.cboEncodings.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboEncodings.FormattingEnabled = true;
            this.cboEncodings.Location = new System.Drawing.Point(110, 12);
            this.cboEncodings.Name = "cboEncodings";
            this.cboEncodings.Size = new System.Drawing.Size(170, 21);
            this.cboEncodings.TabIndex = 1;
            // 
            // panelDrop
            // 
            this.panelDrop.AllowDrop = true;
            this.panelDrop.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.panelDrop.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelDrop.Controls.Add(this.lblDragHere);
            this.panelDrop.Location = new System.Drawing.Point(12, 39);
            this.panelDrop.Name = "panelDrop";
            this.panelDrop.Size = new System.Drawing.Size(447, 205);
            this.panelDrop.TabIndex = 2;
            this.panelDrop.DragDrop += new System.Windows.Forms.DragEventHandler(this.panelDrop_DragDrop);
            this.panelDrop.DragEnter += new System.Windows.Forms.DragEventHandler(this.panelDrop_DragEnter);
            // 
            // lblDragHere
            // 
            this.lblDragHere.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.lblDragHere.Font = new System.Drawing.Font("Arial", 24F);
            this.lblDragHere.ForeColor = System.Drawing.SystemColors.ButtonShadow;
            this.lblDragHere.Location = new System.Drawing.Point(-1, 0);
            this.lblDragHere.Name = "lblDragHere";
            this.lblDragHere.Size = new System.Drawing.Size(447, 204);
            this.lblDragHere.TabIndex = 0;
            this.lblDragHere.Text = "Drag && Drop Files to Convert";
            this.lblDragHere.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // lblTargetEncoding
            // 
            this.lblTargetEncoding.AutoSize = true;
            this.lblTargetEncoding.Location = new System.Drawing.Point(12, 15);
            this.lblTargetEncoding.Name = "lblTargetEncoding";
            this.lblTargetEncoding.Size = new System.Drawing.Size(92, 13);
            this.lblTargetEncoding.TabIndex = 4;
            this.lblTargetEncoding.Text = "Target Encoding :";
            // 
            // chkAutoName
            // 
            this.chkAutoName.AutoSize = true;
            this.chkAutoName.Checked = true;
            this.chkAutoName.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkAutoName.Location = new System.Drawing.Point(286, 14);
            this.chkAutoName.Name = "chkAutoName";
            this.chkAutoName.Size = new System.Drawing.Size(175, 17);
            this.chkAutoName.TabIndex = 5;
            this.chkAutoName.Text = "Automaticaly Name Target Files";
            this.chkAutoName.UseVisualStyleBackColor = true;
            // 
            // statusStrip1
            // 
            this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsStatus,
            this.tsOutputFile});
            this.statusStrip1.Location = new System.Drawing.Point(0, 251);
            this.statusStrip1.Name = "statusStrip1";
            this.statusStrip1.Size = new System.Drawing.Size(471, 22);
            this.statusStrip1.TabIndex = 6;
            this.statusStrip1.Text = "statusStrip1";
            // 
            // tsStatus
            // 
            this.tsStatus.Name = "tsStatus";
            this.tsStatus.Size = new System.Drawing.Size(0, 17);
            // 
            // tsOutputFile
            // 
            this.tsOutputFile.Name = "tsOutputFile";
            this.tsOutputFile.Size = new System.Drawing.Size(0, 17);
            // 
            // Master
            // 
            this.AllowDrop = true;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(471, 273);
            this.Controls.Add(this.statusStrip1);
            this.Controls.Add(this.chkAutoName);
            this.Controls.Add(this.lblTargetEncoding);
            this.Controls.Add(this.panelDrop);
            this.Controls.Add(this.cboEncodings);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MinimumSize = new System.Drawing.Size(479, 300);
            this.Name = "Master";
            this.Text = "File Conversion";
            this.Load += new System.EventHandler(this.Master_Load);
            this.panelDrop.ResumeLayout(false);
            this.statusStrip1.ResumeLayout(false);
            this.statusStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.SaveFileDialog saveFileDlg;
        private System.Windows.Forms.ComboBox cboEncodings;
        private System.Windows.Forms.Panel panelDrop;
        private System.Windows.Forms.Label lblTargetEncoding;
        private System.Windows.Forms.CheckBox chkAutoName;
        private System.Windows.Forms.StatusStrip statusStrip1;
        private System.Windows.Forms.ToolStripStatusLabel tsStatus;
        private System.Windows.Forms.ToolStripStatusLabel tsOutputFile;
        private System.Windows.Forms.Label lblDragHere;
 
    }
Personal tools