勉強させてください。 vb2010.net win764bit FTPサーバーにアクセスし、FTPサーバーにあるCSVファイルをすべて 指定のフォルダにコピーしたいのです。 http://dobon.net/vb/dotnet/internet/ftpwebrequest.html#downloadfile を参考に作成しましたがうまくいきません。 1, Cドライブにコピーしたいのですが下記部分でアクセスに拒否されましたと エラーが発生します。 Dim fs As New System.IO.FileStream( _ downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write) フォルダの設定をPrivate Const PATH_Folder = " C:\DATA"から Private Const PATH_Folder = " \DATA"に変更するとサーバー上にアクセス(ファイルの書き込み) が行われます。(プログラムの保存先が影響?) 指定したローカルにコピーするにはどうしららよいでしょうか。 2,1でサーバー上にファイルの書き込みができたのですが、FTPサーバーにあるCSVのデータの内容を 書くみたいでコピーできませんでした。 fs.Write(buffer, 0, readSize)をfs.CopyTo()に変更すればいいのではないかとおもい変更してみましたが、 fs.CopyTo(ここがわからない)が理解できずに動作しません。 アドバイスよろしくお願いいたします。 Imports System.Net Imports System.IO Module Module1 'FTP用host Private Const FTP_HOST = "ftp://TEST" 'FTP用USER Private Const FTP_USER = "ftpuser" 'FTP用PASSWORD Private Const FTP_PASS = "ftppass" 'FTPファイルコピー先フォルダ Private Const PATH_Folder = " C:\DATA" Private Function FTPDownLoad() As Boolean FTPDownLoad = False 'Try 'ダウンロードするファイルのURI Dim u As New Uri(FTP_HOST) 'ダウンロードしたファイルの保存先 Dim downFile As String = PATH_Folde Sub Main() '----------------------------- '-- FTP データ取得 '----------------------------- If FTPDownLoad() = False Then body = "FTPからのデータダウンロードに失敗しました。" + vbCrLf + sys_date & sys_time Exit Sub End If Private Function FTPDownLoad() As Boolean FTPDownLoad = False 'Try 'ダウンロードするファイルのURI Dim u As New Uri(FTP_HOST) 'ダウンロードしたファイルの保存先 Dim downFile As String = Palet_PATH_Folder 'FtpWebRequestの作成 Dim ftpReq As System.Net.FtpWebRequest = _ CType(System.Net.WebRequest.Create(u), System.Net.FtpWebRequest) 'ログインユーザー名とパスワードを設定 ftpReq.Credentials = New System.Net.NetworkCredential(FTP_USER, FTP_PASS) 'MethodにWebRequestMethods.Ftp.DownloadFile("RETR")を設定 'ftpReq.Method = System.Net.WebRequestMethods.Ftp.DownloadFile ftpReq.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails '要求の完了後に接続を閉じる ftpReq.KeepAlive = False 'ASCIIモードで転送する ftpReq.UseBinary = False 'PASSIVEモードを無効にする ftpReq.UsePassive = False 'FtpWebResponseを取得 Dim ftpRes As System.Net.FtpWebResponse = _ CType(ftpReq.GetResponse(), System.Net.FtpWebResponse) 'ファイルをダウンロードするためのStreamを取得 Dim resStrm As System.IO.Stream = ftpRes.GetResponseStream() 'ダウンロードしたファイルを書き込むためのFileStreamを作成 Dim fs As New System.IO.FileStream( _ downFile, System.IO.FileMode.Create, System.IO.FileAccess.Write) 'ダウンロードしたデータを書き込む Dim buffer(1023) As Byte While True Dim readSize As Integer = resStrm.Read(buffer, 0, buffer.Length) If readSize = 0 Then Exit While End If fs.Write(buffer, 0, readSize) End While fs.Close() resStrm.Close() 'FTPサーバーから送信されたステータスを表示 Console.WriteLine("{0}: {1}", ftpRes.StatusCode, ftpRes.StatusDescription) '閉じる ftpRes.Close() 'Catch ex As Exception ' MsgBox(ex.Message) ' Exit Function 'End Try FTPDownLoad = True End Function END sub
↧