参数异常 - 路径不是合法形式 (vb.net)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14739756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:14:19  来源:igfitidea点击:

Argument Exception - The path is not of a legal form (vb.net)

vb.netargumentexception

提问by Chris Stone

I'm currently having the most irritating error in a program i'm making and i would seriously appreciate any help or advice that could help me fix it. The part of the program that i'm having a problem with is a form that loads up a selected image into a picturebox and then saves it into an MS Access database upon the click of the 'save' button. When executing the "Browse_Click" event, it prompts you to search for an image location and loads it into a picturebox (pbImage). This bit works fine and successfully loads it into it picturebox. The problem i'm having is when i try to save the image to my access database, i get the following argument exception error "The path is not of a legal form". As far as i know all my code is fully functional because it previously worked, however an hour or two ago this error suddenly started appearing.

我目前在我正在制作的程序中遇到了最令人恼火的错误,我非常感谢任何可以帮助我修复它的帮助或建议。我遇到问题的程序部分是一个表单,它将选定的图像加载到一个图片框中,然后在单击“保存”按钮后将其保存到 MS Access 数据库中。执行“Browse_Click”事件时,它会提示您搜索图像位置并将其加载到图片框(pbImage)中。该位工作正常并成功将其加载到图片框中。我遇到的问题是当我尝试将图像保存到我的访问数据库时,我收到以下参数异常错误“路径不合法”。据我所知,我的所有代码都是功能齐全的,因为它以前有效,

The first section of code below is what is executed when i want to load the picture into the picture box. The section below that is the 'save' code.

下面的第一段代码是当我想将图片加载到图片框中时执行的代码。下面的部分是“保存”代码。

Public Class Manage_Cottages
Dim imgName As String
Dim daImage As OleDbDataAdapter
Dim dsImage As DataSet
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
    Dim dlgImage As FileDialog = New OpenFileDialog()
    dlgImage.Filter = "Image File (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif"
    If dlgImage.ShowDialog() = DialogResult.OK Then
        imgName = dlgImage.FileName
        Dim selectedFileName As String = dlgImage.FileName
        txtPath.Text = selectedFileName
        Dim newimg As New Bitmap(imgName)
        pbImage.SizeMode = PictureBoxSizeMode.StretchImage
        pbImage.Image = DirectCast(newimg, Image)
    End If
    dlgImage = Nothing
    imgName = " "
End Sub'

Save code

保存代码

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\Debug\CourseworkDatabase.mdb"
    Dim CN As New OleDbConnection(cnString)
    CN.Open()

     If imgName <> "" Then
        Dim fs As FileStream
        fs = New FileStream(imgName, FileMode.Open, FileAccess.Read) <----- where the error occurs.
        Dim picByte As Byte() = New Byte(fs.Length - 1) {}
        fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
        fs.Close()
        Dim strSQL As String
        strSQL = "INSERT INTO Cottage_Details([Image]) values (" & " @Img)"
        Dim imgParam As New OleDbParameter()
        imgParam.OleDbType = OleDbType.Binary
        imgParam.ParameterName = "Img"
        imgParam.Value = picByte
        Dim cmd As New OleDbCommand(strSQL, CN)
        cmd.Parameters.Add(imgParam)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Image successfully saved.")
        cmd.Dispose()
        CN.Close()
    End If
End Sub

Also below is the first couple of lines of what's displayed in the immediate window (not sure whether it will be of any help to diagnose the problem)

下面也是即时窗口中显示内容的前几行(不确定它是否有助于诊断问题)

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAlphaHolidayCottages.vshost.exeSystem.ArgumentException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089The path is not of a legal form. at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)

mscorlib.dll System.Transactions Critical: 0 : http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/UnhandledUnhandled exceptionAlphaHolidayCottages 中发生“System.ArgumentException”类型的第一次机会异常.vshost.exeSystem.ArgumentException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 路径不合法。在 System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength)

Thanks for your time and help, would be over the moon if someone could help me resolve the issue.

感谢您的时间和帮助,如果有人能帮我解决问题,我会欣喜若狂。

Chris

克里斯

回答by xpda

You set imgName to " " at the end of btnBrowse_Click, so when you save the file under btnSave_Clickyou are trying to save it to the file name " ".

您将 imgName 在末尾设置为“” btnBrowse_Click,因此当您保存文件时,btnSave_Click您试图将其保存为文件名“”。

Try removing imgName = " "at the end of btnBrowse_Click, or assign imgName a proper file name before you save it.

尝试imgName = " "在 末尾删除btnBrowse_Click,或在保存之前为 imgName 分配一个正确的文件名。