The title may not be as clear so let me elaborate a little bit. I am creating an application that minifies CSS and JS files for web developers. All of the code is working perfectly except for one little pesky bug I can not figure out! Here is my code and then I will explain more about my issue:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ZippCast_Minify
{
    public partial class Form1 : DevComponents.DotNetBar.Office2007Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Select the File You Want to Minify";
            ofd.Filter = "CSS Files (.css)|*.css|Javascript Files (.js)|*.js";
            ofd.FilterIndex = 1;
            DialogResult result = ofd.ShowDialog();

            if (result == DialogResult.OK)
            {
                textBoxX1.Text = File.ReadAllText(ofd.FileName);
                toolStripStatusLabel1.Text = "File Opened - " + ofd.FileName;
                buttonX1.Enabled = true;
            }
            else
            {
                MessageBox.Show("Error: Please try again!", "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Title = "Save the Minified Document";
            sfd.Filter = "Minified CSS (*.min.css)|*.min.css|Minified Javascript (*.min.js)|*.min.js";
            sfd.SupportMultiDottedExtensions = true;
            DialogResult result = sfd.ShowDialog();

            if (result == DialogResult.OK)
            {
                File.WriteAllText(sfd.FileName, textBoxX1.Text);
                MessageBox.Show("Your file has been minified! You can find your file at:\n\n" + sfd.FileName, "File Minified Successfully", MessageBoxButtons.OK, MessageBoxIcon.Information);
                buttonX1.Enabled = false;
                buttonX2.Enabled = false;
                buttonX3.Enabled = true;
                toolStripStatusLabel1.Text = "File Opened - N/A";
                textBoxX1.Text = "";
            }
            else
            {
                MessageBox.Show("Error: Please try again!", "Error Saving File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            textBoxX1.Text = textBoxX1.Text.Replace(Environment.NewLine, "");
            textBoxX1.Text = textBoxX1.Text.Replace(" ", "");
            buttonX2.Enabled = true;
            buttonX1.Enabled = false;
            buttonX3.Enabled = false;
        }
    }
}

So, simple enough right? But when I am saving the document if I type in a file name and change the filter to the one I want to use, it inserts .min into the filename box as the extension. And if that wasn't bad enough, if you keep clicking them it will keep adding .min to the file. I am not sure why this is and any help would be awesome! Thanks guys!

Recommended Answers

All 4 Replies

I couldn't replicate it on my machine, but I'm using Mono so that is to be expected. It only added extras when I failed to type in the complete extension.

Like: file.min.css yields file.min.css

..but file.min yields file.min.min.css

I did however see this AddExtension setting you might want to take a look at. Also, do you have DefaultExt set? It could possibly interfere.

If it's set to MultiExtension, and its adding extensions, then it might think you actually want file.min.min.min.css, though I admit it should be a little smarter than that.

I tried adding the following snippet to my code:

sfd.AddExtension = false;

And I also removed this snippet:

sfd.SupportMultiDottedExtensions = true;

Yet everytime I change the filter in the filter dropdown of the save dialog it keeps adding .min to the text already in the file name box. This bug has gotten me stumped and I have been programming in C# for a while now. >:(

The only other thing I can think of is the 'DefaultExt' setting. Which should be an empty string. Sorry.

I tried that with no avail. Thanks for your help though! I will continue to try and debug the issue and will post back if I figure anything out!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.