C#空路径名不合法-Winform
下面是我的windows窗体上的“添加”按钮。当我试图在不添加任何图像的情况下单击它时&也没有路径,出现了我在下面代码中提到的错误。我想修复这个异常,即使用户不添加图像或文件路径,也不会得到异常。我知道它被问了很多次,但他们的代码中的异常是不同的,所以我有点困惑。多谢各位C#空路径名不合法-Winform,c#,winforms,C#,Winforms,下面是我的windows窗体上的“添加”按钮。当我试图在不添加任何图像的情况下单击它时&也没有路径,出现了我在下面代码中提到的错误。我想修复这个异常,即使用户不添加图像或文件路径,也不会得到异常。我知道它被问了很多次,但他们的代码中的异常是不同的,所以我有点困惑。多谢各位 private void btn_add_Click(object sender, EventArgs e) { byte[] image = null; var stream = ne
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);
您可以预先检查文件是否存在:
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
时,您需要显式地关闭它,并确保已将其处理掉以删除打开的文件句柄,以便其他应用程序可以访问该文件。您可以通过调用Close和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
FileStream
和BinaryReader
对象(stream
和read
)只存在于使用语句关闭括号}
的地方。您似乎已经回答了自己的问题。“没有添加任何图像&没有路径”-是的,这正是错误告诉您的。“当然,我会将其验证为空字段”-是的,这是您需要做的。那么到底是什么问题呢?在尝试使用该值之前,您是否尝试添加任何代码来检查该值是否为空?什么不起作用?@David我试过了,先生,string.IsNullOrEmpty(txt_path.Text)不起作用你在哪里试过这个?因为这正是这里的任何人验证该值的方式,如果使用正确,它将正常工作。那么你尝试了什么,怎么失败了?@David你可以在我的代码中看到它,先生。我试图验证我的文本框,其中一个是我的文件路径,它验证了除文件路径的文本框之外的所有文本框。它是怎么失败的?什么都没发生,只是发生了异常。@Diether:“您可以在我的代码中看到它,先生。”-您在代码中的什么地方这样做?错误发生在方法的第二行。你肯定不是在第一行这样做的。那你在哪里做这个?它失败的原因似乎是因为您试图在错误发生后阻止它。相反,尝试在错误发生之前阻止它。首先验证输入,然后使用输入。与其尝试使用输入,然后稍后验证它。我会添加一个检查文件是否打开。也许,但问题是,当路径为空时,为什么它会失败,所以我不会通过添加无关代码来混淆,而是将其保持在最小值以回答问题。我还可以建议使用语句包装FileStream和BinaryReader,以确保它们得到正确处理,但这离回答眼前的问题越来越远了。使用try…catch方法将处理文件已经打开的问题。这就是我一直在寻找的答案。非常感谢,先生。关于您在这里提到的先生,“我还可以包括一些建议,在使用语句时包装FileStream和BinaryReader,以确保它们得到正确处理”我很想听听你的建议,我知道这已经不可能了,但作为一名开发人员,我知道我已经更新了答案,加入了一些关于使用
语句的。