简体   繁体   English

C#System.ArgumentException:“空路径名不合法。”

[英]C# System.ArgumentException: “Empty path name is not legal.”

I'm messing about with winforms and zip programs. 我搞砸了winforms和zip程序。 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) 我使用以下方法解决了此问题:(尽管,如果我输入name.rar,它可以正常工作,但是我们当然希望.rar部分是自动的)

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. 现在,它迫使.rar作为扩展名,尽管它位于“另存为类型”下拉列表中,而不是名称本身。

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. 它会引发“空路径名不合法”错误,如果我在末尾输入.rar,它也会做同样的事情。

Here's the rest of the code of the button; 这是按钮的其余代码; the others work fine. 其他工作正常。 It happens at the ZipArchive zip = ZipFile.Open line. 它发生在ZipArchive zip = ZipFile.Open行。

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!");
    }
}

Based on your findings in the debug. 根据您在调试中的发现。

If FileName is blank, you will get this error. 如果FileName为空白,则会出现此错误。 You need to input a filename. 您需要输入文件名。

You can add extra check: 您可以添加额外的检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM