VB.NET 中的文件夹被拒绝访问

Posted

技术标签:

【中文标题】VB.NET 中的文件夹被拒绝访问【英文标题】:Access Denied to Folder in VB.NET 【发布时间】:2016-01-22 18:58:41 【问题描述】:

我正在尝试从 ftp 下载 .csv 文件,然后将数据从下载的文件导入 sql 表。但是它给了我错误“拒绝访问路径”,但是当我在没有代码的情况下访问它时,我可以看到文件并且文件也没有从 ftp 下载。

Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.SqlClient



Public Class Form1

Sub Get_File_From_FTP()

    Try

        Dim RFN = "ftp://someaddress.com/Test.csv"
        Dim LFN = "C:\Sample\IncomingFiles\"
        Dim FTP As FtpWebRequest = CType(FtpWebRequest.Create(RFN), FtpWebRequest)
        FTP.Credentials = New NetworkCredential("ftpdatasandyalexander", "Y$m0z1k30")
        FTP.KeepAlive = False
        FTP.UseBinary = True
        FTP.Method = WebRequestMethods.Ftp.DownloadFile
        Using FtpResponse As FtpWebResponse = CType(FTP.GetResponse, FtpWebResponse)
            Using ResponseStream As IO.Stream = FtpResponse.GetResponseStream

                Using fs As New IO.FileStream(LFN, FileMode.Create)
                    Dim buffer(2047) As Byte
                    Dim read As Integer = 0
                    Do
                        read = ResponseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)
                    Loop Until read = 0
                    ResponseStream.Close()
                    fs.Flush()
                    fs.Close()
                    'Log("")
                End Using
                ResponseStream.Close()
                MessageBox.Show("File Downloaded!")

            End Using
        End Using

    Catch ex As Exception
        MessageBox.Show(ex.Message)

    End Try
End Sub

Sub Import_File_To_SQL()
    Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Sample\IncomingFiles\Test.csv;Extended Properties=""text;HDR=Yes;FMT=Delimited(;)"";Persist Security Info=False")
    ExcelConnection.Open()

    Dim expr As String = "Select * From [sheet$]"

    Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
    Dim objDR As OleDbDataReader

    Dim SQLconn As New SqlConnection()
    Dim ConnString As String = "Data Source=MMSQL1;Initial Catalog=dbname; User Id=user; Password=pass;"
    SQLconn.ConnectionString = ConnString
    SQLconn.Open()


    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
        bulkCopy.DestinationTableName = "tDHL_SortCode"

        Try
            objDR = objCmdSelect.ExecuteReader
            bulkCopy.WriteToServer(objDR)
            objDR.Close()
            SQLconn.Close()
            MessageBox.Show("File Imported!")

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Using
End Sub

Private Sub btndownload_Click(sender As Object, e As EventArgs) Handles btndownload.Click
    Call Get_File_From_FTP()
    Call Import_File_To_SQL()
End Sub

结束类

【问题讨论】:

【参考方案1】:

您正在尝试将文件下载到目录中而未提供文件名。问题在于这里的代码:

Dim LFN = "C:\Sample\IncomingFiles\"

它应该是文件路径而不是目录路径。

试试

Dim LFN = "C:\Sample\IncomingFiles\Test.csv"

【讨论】:

文件应该在那里下载,如果有同名文件则会报错 那么在这种情况下,您只需添加代码以检查文件是否存在。如果存在,则将其删除。 If IO.File.Exists(LFN) Then IO.File.Delete(LFN)【参考方案2】:

这是我为解决问题而编写的代码

Imports System.Net
Imports System.Text
Imports System.IO
Imports System.Data.OleDb
Imports System.Data.SqlClient



Public Class Form1

Sub Get_File_From_FTP()

    Dim buffer(1023) As Byte ' 
    Dim bytesIn As Integer ' 
    Dim totalBytesIn As Integer '
    Dim output As IO.Stream '
    Try
        Dim FTPRequest As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://something.com/Sort_Codes/" & "sample.csv"), System.Net.FtpWebRequest)
        FTPRequest.Credentials = New System.Net.NetworkCredential("user", "pass")
        FTPRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
        Dim stream As System.IO.Stream = FTPRequest.GetResponse.GetResponseStream
        output = System.IO.File.Create("C:\Test\IncomingFiles\Test.csv")
        bytesIn = 1
        Do Until bytesIn < 1
            bytesIn = stream.Read(buffer, 0, 1024)
            If bytesIn > 0 Then
                output.Write(buffer, 0, bytesIn)
                totalBytesIn += bytesIn
                Label2.Text = totalBytesIn.ToString + " Bytes Downloaded"
                Application.DoEvents()
            End If
        Loop
        output.Close()
        stream.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub


Sub Import_File_To_SQL()
    Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & "C:\Test\IncomingFiles" & ";" & _
        "Extended Properties=""Text;HDR=YES;IMEX=1;""")
    ExcelConnection.Open()

    Dim expr As String = "Select * From [Test.csv]"

    Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection)
    Dim objDR As OleDbDataReader

    Dim SQLconn As New SqlConnection()
    Dim ConnString As String = "Data Source=server;Initial Catalog=dbname; User Id=user; Password=pass;"
    SQLconn.ConnectionString = ConnString
    SQLconn.Open()


    Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn)
        bulkCopy.DestinationTableName = "tDHL_SortCode"

        Try
            objDR = objCmdSelect.ExecuteReader
            bulkCopy.WriteToServer(objDR)
            objDR.Close()
            SQLconn.Close()
            MessageBox.Show("File Imported!")

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Using
End Sub

Private Sub btndownload_Click(sender As Object, e As EventArgs) Handles btndownload.Click
    Call Get_File_From_FTP()
    Call Import_File_To_SQL()
End Sub

结束类

【讨论】:

以上是关于VB.NET 中的文件夹被拒绝访问的主要内容,如果未能解决你的问题,请参考以下文章

如何将管理员权限添加到 VB.net?访问被拒绝尝试用 VB.net 替换 Systems32\Hosts 文件

XP系统可以看到共享文件夹却拒绝访问

以 Android SDK 版本 29 为目标后,读取/写入下载文件夹被拒绝

为啥使用CMD命令中的find命令总是拒绝访问?

文件无法打开访问被拒绝 怎么解决

拒绝访问 .htaccess 中的一个特定文件夹