GhostyIs1337
GhostyIs1337

Reputation: 33

C# System.ArgumentException: "Empty path name is not legal."

I'm messing about with winforms and zip programs. Now, the first hiccup I had was that it didn't force any extension when I tried to save it, so if I typed in a name it just saved it as a file. I fixed this with: (although, if I typed in name.rar it worked fine, but we want the .rar part to be automatic, of course)

saveFileDialog1.Title = "Izberi kam naj se datoteke kompresirajo";
saveFileDialog1.DefaultExt = "rar";
saveFileDialog1.Filter = "RAR Files (*.rar)|*.rar";
saveFileDialog1.FilterIndex = 1;

Now it forces .rar as an extension, although, it's in the "Save as type" drop-down, not in the name itself.

When I try to save it with just a name; it throws the "empty path name is not legal" error, it does the same thing if I input .rar at the end.

Here's the rest of the code of the button; the others work fine. It happens at the ZipArchive zip = ZipFile.Open line.

private void button3_Click(object sender, EventArgs e)
{

    saveFileDialog1.Title = "Izberi kam naj se datoteke kompresirajo";
    saveFileDialog1.DefaultExt = "rar";
    saveFileDialog1.Filter = "RAR Files (*.rar)|*.rar";
    saveFileDialog1.FilterIndex = 1;


    DialogResult result = saveFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        if (isFolder)
        {
            ZipFile.CreateFromDirectory(textBox1.Text, saveFileDialog1.FileName);
        }
        else
        {
            string[] files = textBox1.Text.Split(',');
            ZipArchive zip = ZipFile.Open(saveFileDialog1.FileName, ZipArchiveMode.Create);
            foreach (string file in files)
            {
                zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
            }
            zip.Dispose();
        }
        MessageBox.Show("Uspešno!");
    }
}

Upvotes: 0

Views: 6084

Answers (1)

Sunil
Sunil

Reputation: 3424

Based on your findings in the debug.

If FileName is blank, you will get this error. You need to input a filename.

You can add extra check:

if (result == DialogResult.OK && !string.IsNullOrEmpty(saveFileDialog1.FileName))

Upvotes: 1

Related Questions