Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello again,

I would like to be able to convert my files back to base64 after I update them.
but I don't want to use regex, I just want to convert all the lines within the quotes.
I have a file that I include in my Programs that when executed it decodes the lines.
I believe we can use the code below, with minor adjustments.

Here is a few of lines of code to test it on:

C#
this.saveToolStripMenuItem.BackColor = System.Drawing.SystemColors.Menu;
            this.saveToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("saveToolStripMenuItem.Image")));
            this.saveToolStripMenuItem.Name = "saveToolStripMenuItem";
            this.saveToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.S)));
            this.saveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.saveToolStripMenuItem.Text = "&Save";
            this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click);
            this.saveAsToolStripMenuItem.BackColor = System.Drawing.SystemColors.Menu;
            this.saveAsToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("saveAsToolStripMenuItem.Image")));
            this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem";
            this.saveAsToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.A)));
            this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.saveAsToolStripMenuItem.Text = "Save &As";
            this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click);
            this.exitToolStripMenuItem.BackColor = System.Drawing.SystemColors.Menu;
            this.exitToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("exitToolStripMenuItem.Image")));
            this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
            this.exitToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.X)));
            this.exitToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.exitToolStripMenuItem.Text = "E&xit";
            this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); 
            this.optionsCtrlPToolStripMenuItem.BackColor = System.Drawing.SystemColors.Control;
            this.optionsCtrlPToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.shareSkinsCtrlHToolStripMenuItem});
            this.optionsCtrlPToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlText;
            this.optionsCtrlPToolStripMenuItem.Name = "optionsCtrlPToolStripMenuItem";
            this.optionsCtrlPToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.P)));
            this.optionsCtrlPToolStripMenuItem.Size = new System.Drawing.Size(61, 20);
            this.optionsCtrlPToolStripMenuItem.Text = "O&ptions"; 
            this.shareSkinsCtrlHToolStripMenuItem.BackColor = System.Drawing.SystemColors.Menu;
            this.shareSkinsCtrlHToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("shareSkinsCtrlHToolStripMenuItem.Image")));
            this.shareSkinsCtrlHToolStripMenuItem.Name = "shareSkinsCtrlHToolStripMenuItem";
            this.shareSkinsCtrlHToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.H)));
            this.shareSkinsCtrlHToolStripMenuItem.Size = new System.Drawing.Size(172, 22);
            this.shareSkinsCtrlHToolStripMenuItem.Text = "S&hare Skins";
            this.shareSkinsCtrlHToolStripMenuItem.Click += new System.EventHandler(this.shareSkinsCtrlHToolStripMenuItem_Click); 
            this.helpToolStripMenuItem.BackColor = System.Drawing.SystemColors.Control;
            this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aboutCtrlBToolStripMenuItem,
            this.checkForUpdateCtrlUToolStripMenuItem});
            this.helpToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlText;
            this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
            this.helpToolStripMenuItem.ShortcutKeys = System.Windows.Forms.Keys.F1;
            this.helpToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
            this.helpToolStripMenuItem.Text = "He&lp";
            this.aboutCtrlBToolStripMenuItem.BackColor = System.Drawing.SystemColors.Menu;
            this.aboutCtrlBToolStripMenuItem.Image = ((System.Drawing.Image)(resources.GetObject("aboutCtrlBToolStripMenuItem.Image")));
            this.aboutCtrlBToolStripMenuItem.Name = "aboutCtrlBToolStripMenuItem";
            this.aboutCtrlBToolStripMenuItem.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.B)));
            this.aboutCtrlBToolStripMenuItem.Size = new System.Drawing.Size(204, 22);
            this.aboutCtrlBToolStripMenuItem.Text = "A&bout";
            this.aboutCtrlBToolStripMenuItem.Click += new System.EventHandler(this.aboutCtrlBToolStripMenuItem_Click);V


What I have tried:

Here us what I use to convert my C# files FromBase64 :

C#
<pre>private void butDecodeFiles_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Open File";
            dlg.Filter = "C Sharp files (*.cs)|*.cs|All files (*.*)|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string line;

                string[] SelectedFiles = dlg.FileNames;
                dlg.Dispose();
                foreach (string file in SelectedFiles)
                {
                    using (StreamReader streamIn = new StreamReader(file))
                    {
                        using (StreamWriter streamOut = new StreamWriter(file + "-copy"))
                        {
                            while ((line = streamIn.ReadLine()) != null)
                            {
                                if (line.Contains("Base64Decode.strongName"))
                                {
                                    Regex rgx = new Regex("Base64Decode\\.strongName\\(\\\"(?<base64>[a-zA-z0-9\\/=]+)\\\"\\)");
                                    MatchCollection mc = rgx.Matches(line);
                                    if (mc.Count > 0)
                                    {
                                        foreach (Match m in mc)
                                        {
                                            string s = m.Groups["base64"].Value;
                                            byte[] data = Convert.FromBase64String(s);
                                            line = line.Replace(m.Value, "\"" + Encoding.Unicode.GetString(data) + "\"");
                                        }
                                    }
                                }
                                streamOut.WriteLine(line);
                            }
                        }
                    }
                    try
                    {
                        File.Delete(file);
                        File.Move(file + "-copy", file);
                        File.Delete(file + "-copy");
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error");
                        throw;
                    }
                }
                MessageBox.Show("Conversion Complete");
            }
        }
Posted
Updated 20-Apr-18 2:12am

1 solution

You can use Directory.GetFiles Method (String) (System.IO)[^] to get the list of files. You then just send each file from the list into your update process. The update process needs to scan the content of each file looking for quoted strings, which it then converts to Base64. It would be a sensible idea when running this to create new versions of each file in a different directory, rather than overwriting the originals.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900