FTPにてアップロード、ダウンロードファイル、デェレクトリの削除をVB.NET 2005で作成しています。アップ、ダウンロードは成功していますが、削除系がまったく上手くいきません。APIを使用しています。どなたか教えて下さい。以下は作成中のプログラムです。
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotsugo.Click
'FTPクラス
Dim ftpFile As New clsFTP
Dim nRet As Integer = -1
'FTPでデェレクトリ配下のPDF/画像ファイルを削除する
Dim LocalFile As String = ""
Dim RemoteFile As String = "/test/test1"
Dim ftpErrMsg As String = ""
nRet = ftpFile.DeleteDir_CreateDir(RemoteFile, ftpErrMsg)
If nRet.Equals(-1) Then
MessageBox.Show("ファイルの削除に失敗しました。エラー:(" & ftpErrMsg & ")")
End If
'FTPサーバの環境設定
Private Const g_Ftp_Host = "192.xx.xx.xx"
Private Const g_Ftp_User = "アノニマス"
Private Const g_Ftp_PassWord = ""
Private Const g_Ftp_CurrentDir = "/test"
#Region "FtpRemoveDirectory...デェレクトリ削除"
<System.Runtime.InteropServices.DllImport("wininet.dll")> _
Private Shared Function FtpRemoveDirectory _
(ByVal hFtpSession As Int32, _
ByVal lpszDirectory As String) As Int32
End Function
#End Region
#Region "FtpDeleteFile...FTPでリモートのファイルを削除"
<System.Runtime.InteropServices.DllImport("wininet.dll")> _
Private Shared Function FtpDeleteFile( _
ByVal hFtpSession As Int32, _
ByVal lpszRemoteFile As String) As Boolean
End Function
#End Region
#Region "FtpSetCurrentDirectory...FTPのリモートのカレントディレクトリを設定"
<System.Runtime.InteropServices.DllImport("wininet.dll")> _
Private Shared Function FtpSetCurrentDirectory( _
ByVal hFtpSession As Int32, _
ByVal lpszDirectory As String) As Int32
End Function
#End Region
#Region "DeleteDir_CreateDir...デェレクトリ削除後、作成"
Public Function DeleteDir_CreateDir(ByVal strRemote As String, ByRef strErrMsg As String) As Integer
nRet = -1 '初期化
Try
'インターネットハンドルの作成
lOpen = InternetOpen(strSClient, INTERNET_OPEN_TYPE_PRECONFIG, strProxyName, strProxyPass, 0&)
If Not lOpen.Equals(0) Then
'インターネット上のサーバへの接続
lSession = InternetConnect(lOpen, g_Ftp_Host, INTERNET_DEFAULT_FTP_PORT, g_Ftp_User, g_Ftp_PassWord, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0&)
If Not lSession.Equals(0) Then
'FTPのリモートのカレントディレクトリを設定
lRet = FtpSetCurrentDirectory(lSession, g_Ftp_CurrentDir)
If Not lRet.Equals(0) Then
''FTPでリモートのデェレクトリを削除
'lRet = FtpRemoveDirectory(lSession, strRemote)
'If Not lRet.Equals(0) Then
'FTPでリモートのデェレクトリ作成
lRet = FtpCreateDirectory(lSession, strRemote)
If Not lRet.Equals(0) Then
nRet = 0
Else
strErrMsg = fncStrGetInetError()
End If
'Else
' strErrMsg = fncStrGetInetError()
'End If '←bRetのEnd If
Else
strErrMsg = fncStrGetInetError()
End If '←lRet.EqualsのEnd If
Else
strErrMsg = fncStrGetInetError()
End If '←lSession.EqualsのEnd If
lRet = InternetCloseHandle(lSession)
Else
strErrMsg = fncStrGetInetError()
End If '←lOpen.EqualsのEnd If
lRet = InternetCloseHandle(lOpen)
Catch ex As Exception
strErrMsg = Me.GetType.Name & ":" & ex.Message
Throw
End Try
Return nRet
End Function
#End Region
FTPの場合は単純に実行結果だけを拾うのではなく、
エラーの場合InternetGetLastResponseInfoで原因を
把握します、なぜならエラーが発生しても、原因が
一方的にこちらのコードの不具合だけでなく、サーバー
がBusyだったりします。
提示されたコードは、コメントアウトがしてあって、
何も削除していない気がするのですが......
一般論ですが、Directoryを消す時は、先ずその下にファイルや
Directoryが有るか否かを判断して、有ればそれを消してから、
Directoryの削除となります。
我龍院さん、ありがとうございます。
InternetGetLastResponseInfoとErr.LastDllErrorの戻り値をみると両方とも「0」で返されます。これは指定したデェレクトリが無いと判断されているんじゃないかと・・・どうでしょうか?
ファイルの削除はできました。(ワイルドカード指定で削除できないんですかね?「/*.txt」みたいに。。。)
また、教えてください。お願いします。
ちなみにこんな感じで調べてみました。
'FTPでリモートのデェレクトリを削除
Dim lngBuffer As Int32 = 0&
Dim strBuffer As String = ""
Dim lngErrorNum As Int32 = 0&
lRet = FtpRemoveDirectory(lSession, strRemote)
lngRC = InternetGetLastResponseInfo(lngErrorNum, strBuffer, lngBuffer)
MessageBox.Show(lngRC & "/" & Err.LastDllError)
If Not lRet.Equals(0) Then
'FTPでリモートのデェレクトリ作成
lRet = FtpCreateDirectory(lSession, strRemote)
If Not lRet.Equals(0) Then
nRet = 0
Else
strErrMsg = fncStrGetInetError()
End If
Else
strErrMsg = fncStrGetInetError()
End If
InternetGetLastResponseInfoの戻り値は
InternetGetLastResponseInfoが正しく実行されたか否かを
返しているだけです。
2番目の引数のバッファに情報が返されます。
>ワイルドカード指定で削除できないんですかね?「/*.txt」みたいに。。。)
出来ません、FtpFindFirstFileやInternetFindNextFileで探しながら消します。
2番目のバッファの情報が・・・分かんなかったです。
検索しながら削除はできました。
ありがとうございました。
やりたいことはできました。
お世話になりました。
ツイート | ![]() |