C#空路径名不合法-Winform

时间:2018-08-27 09:35:18

标签: c# winforms

此代码下面是Windows窗体上的“添加”按钮。当我尝试单击它而不添加任何图像并且也没有路径时,发生了我在这些代码下面提到的错误。我想修复此异常,即使用户不添加图像或文件路径也不会获得异常。我知道它被问过很多次了,但是它们在代码中的异常是不同的,所以我有点困惑。谢谢

private void btn_add_Click(object sender, EventArgs e)
    {
        byte[] image = null;
        var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
        var read = new BinaryReader(stream);
        image = read.ReadBytes((int)stream.Length);


        using (var con = SQLConnection.GetConnection())
        {


            if (string.IsNullOrEmpty(cbox_supplier.Text) || string.IsNullOrEmpty(txt_code.Text) || string.IsNullOrEmpty(txt_item.Text) || string.IsNullOrEmpty(txt_quantity.Text) || string.IsNullOrEmpty(txt_cost.Text) || string.IsNullOrEmpty(txt_path.Text))
            {
                MetroMessageBox.Show(this, "Please input the Required Fields", "System Message:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {

                var selectCommand = new SqlCommand("Insert into employee_product (Image, Supplier, Codeitem, Itemdescription, Date, Quantity, Unitcost) Values (@Image, @Supplier, @Codeitem, @Itemdescription, @Date, @Quantity, @Unitcost)",con);
                selectCommand.Parameters.AddWithValue("@Image", image);
                selectCommand.Parameters.AddWithValue("@Supplier", cbox_supplier.Text);
                selectCommand.Parameters.AddWithValue("@Codeitem", txt_code.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Itemdescription", txt_item.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Date", txt_date.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Quantity", txt_quantity.Text.Trim());
                selectCommand.Parameters.AddWithValue("@Unitcost", txt_cost.Text.Trim());
                selectCommand.ExecuteNonQuery();
                MessageBox.Show("Added successfully", "SIMS", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                txt_path.Clear();
                pictureBox1.Image = null;
                txt_code.Clear();
                txt_item.Clear();
                txt_quantity.Clear();
                txt_cost.Clear();
                _view.AddingProduct();

            }

        }
    }
   private void btn_upload_Click(object sender, EventArgs e)
    {
        OpenFileDialog opnfd = new OpenFileDialog();
        opnfd.Filter = "Image Files (*.jpg;*.jpeg;.*.gif;*.png;)|*.jpg;*.jpeg;.*.png;*.gif";
        opnfd.Title = "Select Item";

        if (opnfd.ShowDialog() == DialogResult.OK)
        {
            var path = opnfd.FileName.ToString();
            txt_path.Text = path;
            pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            pictureBox1.Image = Image.FromFile(opnfd.FileName);

        }
    }

//这是发生系统参数异常的地方

        byte[] image = null;
 -----> var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
        var read = new BinaryReader(stream);
        image = read.ReadBytes((int)stream.Length);

1 个答案:

答案 0 :(得分:1)

您可以预先检查文件是否存在:

if (File.Exists(txt_path.Text))
{
    var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
    var read = new BinaryReader(stream);
    image = read.ReadBytes((int)stream.Length);
    // The rest of your code
}

或在发生错误时捕获错误:

try
{
    var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read);
    var read = new BinaryReader(stream);
    image = read.ReadBytes((int)stream.Length);
    // The rest of your code
}
catch
{
    // Creating filestream object failed.
}

您问过如何将FileStream包裹在using语句中

打开FileStream时,需要明确将其关闭,并确保已将其丢弃以删除打开的文件句柄-以便其他应用程序可以访问该文件。您可以通过调用Close and Dispose来执行此操作,也可以将对象包装在using语句中,该语句将自动为您调用close和dispose。

using (var stream = new FileStream(this.txt_path.Text, FileMode.Open, FileAccess.Read))
{
    using (var read = new BinaryReader(stream))
    {
        image = read.ReadByres((int)stream.Length);
    } // BinaryReader is Closed and Disposed here
} // FileStream is Closed and Disposed here

FileStreamBinaryReader对象(streamread)仅存在到using语句结束括号}为止是。