system.notsupportedexception给定路径';不支持s格式

本文关键字:不支持 格式 notsupportedexception 路径 system | 更新日期: 2023-09-27 18:30:03

在我的聊天程序中,我需要将文件发送给其他用户,但我遇到了问题。当我试图发送我的文件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);
        }
    }
}

请帮忙。

system.notsupportedexception给定路径';不支持s格式

尝试File.ReadAllBytes(openFileDialog1.FileName);

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