【问题标题】:system.notsupportedexception the given path's format is not supportedsystem.notsupportedexception 不支持给定路径的格式
【发布时间】:2017-11-18 04:12:26
【问题描述】:

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

请帮忙。

【问题讨论】:

  • openFileDialog1.ToString() 不会返回所选文件的路径 - 它会返回 System.Windows.Forms.OpenDialog(对象的类型)。你想要openDialog1.FileName
  • 获取路径文件后抛出异常

标签: c# .net chat notsupportedexception


【解决方案1】:

试试File.ReadAllBytes(openFileDialog1.FileName);

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

【讨论】:

  • 我想将 opendialog1 的值存储在字符串变量中吗?
  • 什么值? openDialog1OpenFileDialog 类的实例 - 它有几个不同的值(属性)。
  • 我要找路径文件把它转换成字节数组。但是我不知道你是怎么得到这个路径文件的。
  • @АндрійПриймак 在您的代码中搜索我在“您正在使用”中提到的代码,并将其替换为我在“尝试”中建议的代码。
猜你喜欢
  • 1970-01-01
  • 2011-11-13
  • 2012-04-21
  • 1970-01-01
  • 2017-09-14
  • 2014-12-08
  • 1970-01-01
  • 2012-05-15
相关资源
最近更新 更多