Home > Back-end >  FilePath is wrong/not supported
FilePath is wrong/not supported

Time:03-25

i´m trying to save a Camera Picture to a folder. When i save it, i become the following message:

"System.NotSupportedException: "The specified path format is not supported."

When i change the Filepath to be just one single string, the saving works fine, but i want a FileName, which is variable.

Maybe i´m just missing something or i am really dumb, but i really dont know what else i can do, heres the Code:

private void button2_Click(object sender, EventArgs e)
    {
        var stamp = DateTime.Now.ToString("G");
        string[] paths = {@"D:\", stamp, ".jpg"};
        string fullpath = Path.Combine(paths);
        if (isCameraRunning)
        {
            Bitmap snapshot = new Bitmap(pictureBox1.Image);

            snapshot.Save(fullpath, ImageFormat.Jpeg);
        }
        else
        {
            MessageBox.Show("Die Kamera konnte kein Bild machen, da die Kamera kein Bild aufnimmt","Warnung", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

CodePudding user response:

The G format specifier for DateTime uses : to separate the hours, minutes and seconds, and you then use this as the file name. Since paths cannot contain : (except as part of the drive letter indicator), you get an exception as your file name is invalid.

(Separately, Path.Combine() is used for combining paths, it does not support constructing a file name out of the name and extension, e.g. Path.Combine(new[] { "foo", ".bar" }) results in foo\.bar, not foo.bar).

  • Related