system.notsupportedexception不支持给定路径的格式

时间:2014-11-12 09:45:54

标签: c# .net chat notsupportedexception

在我的聊天程序中,我需要将文件发送给其他用户,但我有问题。 当我尝试发送我的文件时,visual studio给我一个例外,不支持给定路径的格式;

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

请帮忙。

1 个答案:

答案 0 :(得分:1)

试试File.ReadAllBytes(openFileDialog1.FileName);

您正在使用openFileDialog1.ToString(),它将返回类似“System.Windows.Forms.OpenFileDialog”的内容,而不是所选文件的路径。