system.notsupportedexception the given path's format is not supported

1.7k views Asked by At

In my chat program I need to send file to other users but I have problem. When I trying to send my file visual studio give me an exception the given path's format is not supported;

private void button3_Click(object sender, EventArgs e)
{
    Stream myStream = null;

    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "A:\\";
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;
    openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer).ToString();

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    byte[] bytes = File.ReadAllBytes(openFileDialog1.ToString());
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error:"+ ex.Message);
        }
    }
}

Please help.

1

There are 1 answers

4
CodeCaster On BEST ANSWER

Try File.ReadAllBytes(openFileDialog1.FileName);.

You're using openFileDialog1.ToString(), which will return something like "System.Windows.Forms.OpenFileDialog", not the path of the selected file.