0

I confess I'm pretty novice at this FTP stuff. I'm trying to download a file from an ftp site, extract data, then delete it. Here is the relevant code (I got the functions from some forum, but haven't been able to find it since):

Public Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal HINet As Integer) As Integer
Public Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Integer, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Integer) As Integer
Public Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Integer, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUsername As String, ByVal sPassword As String, ByVal lService As Integer, ByVal lFlags As Integer, ByVal lContext As Integer) As Integer
Public Declare Function FtpGetFile Lib "wininet.dll" Alias "FtpGetFileA" (ByVal hFtpSession As Integer, ByVal lpszRemoteFile As String, ByVal lpszNewFile As String, ByVal fFailIfExists As Boolean, ByVal dwFlagsAndAttributes As Integer, ByVal dwFlags As Integer, ByVal dwContext As Integer) As Boolean

Sub Main()
    INet = InternetOpen("MyFTP Control", 1, vbNullString, vbNullString, 0)
    INetConn = InternetConnect(INet, "ftp.myftpsite.com", 0, "username", "password", 1, 0, 0)
    FtpGetFile(INetConn, "test.csv", "C:\temp.csv", True, 1, 0, 0)
    InternetCloseHandle(INetConn)
    InternetCloseHandle(INet)

    'do stuff with file

    Kill("C:\temp.csv")

End Sub

It downloads the file just fine, but I get the following error at the kill command: "Access to the path 'C:\temp.csv' is denied." Same error if I use System.IO.File.Delete (see code section at the end for the Exception Detail).

What's strange is that I can delete the file manually with no problem. Also, if I copy the file I can kill the copy, but still not the original. I don't believe it's a connection/releasing issue, because I also cannot kill files downloaded several days & several computer reboots ago. I know it's not a directory privileges issue because I can manually move the file to the desktop, but still cannot kill it via code.

I've found a handful of forum postings with the same problem, but no posted answers. Just to clarify, I'm not trying to delete the file from the ftp site, but the downloaded file on my machine.

I'm using Visual Studio 2008. Thanks in advance!

Exception Detail:

System.UnauthorizedAccessException was unhandled
  Message="Access to the path 'C:\temp.csv' is denied."
  Source="Microsoft.VisualBasic"
  StackTrace:
       at Microsoft.VisualBasic.FileSystem.Kill(String PathName)    at Project1.Module1.Main()    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)    at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
4

1 回答 1

0

使用 AppData 文件夹,问题应该会消失。您的应用不会自动拥有所有文件夹中所有文件的所有权。

 mUserFileDir = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & _
 YourFolderName & YourFileName

Environment.SpecialFolder.LocalApplicationData取决于它在做什么以及为谁做。

也可以检查文件属性和权限。那里可能有什么东西。

于 2013-10-02T15:12:49.223 回答