繁体   English   中英

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

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

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

请帮忙。

尝试File.ReadAllBytes(openFileDialog1.FileName);

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM