【问题标题】:VB.NET - UnauthorizedAccessException when trying to write a file to driveVB.NET - 尝试将文件写入驱动器时出现 UnauthorizedAccessException
【发布时间】:2014-02-07 10:02:30
【问题描述】:

我正在遵循其他post 中描述的方法来存储和检索 MSSQL 2008 数据库中的任何文件。一切正常,除了当我尝试将文件保存到磁盘上的任何位置时,我得到 UnauthorizedAccessException。以下是到目前为止所做的事情

  1. 硬编码路径到“C:\Temp”和“D:\” - UnauthorizedAccessException
  2. 尝试以管理员身份运行 build .exe - 程序意外关闭

谁能指出我解决这个问题的正确方向?

我在 Windows 8 上,这是我正在使用的代码;

Public Sub downloadLearningObject(learningObjectID As Integer, folderPath As String) Implements ILearningObjectDAO.downloadLearningObject
    Dim connection As String = DatabaseConnection.ConnectionString
    Using con As New SqlConnection(connection)
        con.Open()
        Dim selectQuery As String = "SELECT File From LearningObject WHERE LearningObjectID=" & learningObjectID

        Dim cmd As New SqlCommand(selectQuery, con)
        Using sqlQueryResult = cmd.ExecuteReader()
            If sqlQueryResult IsNot Nothing Then
                sqlQueryResult.Read()
                Dim blob = New [Byte]((sqlQueryResult.GetBytes(0, 0, Nothing, 0, Integer.MaxValue)) - 1) {}
                sqlQueryResult.GetBytes(0, 0, blob, 0, blob.Length)

                'the following line is producing the exception
                Using fs = New FileStream(folderPath, FileMode.OpenOrCreate, FileAccess.ReadWrite) 
                    fs.Write(blob, 0, blob.Length)
                End Using
            End If
        End Using

    End Using
End Sub

【问题讨论】:

    标签: wpf vb.net file windows-8 unauthorizedaccessexcepti


    【解决方案1】:

    我发现我的“路径”字符串的格式是错误的。它必须是“C:\Temp\”而不是“C:\Temp”。字符串末尾缺少的反斜杠触发了异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2013-08-09
      • 2015-04-24
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多