Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

FilePath is wrong/not supported

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading