我承认我对这个FTP的东西很新手。我正在尝试从ftp站点下载文件,提取数据,然后将其删除。这是相关的代码(我从一些论坛获得了这些功能,但从那以后就找不到了):
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
它下载文件就好了,但我在kill命令中收到以下错误:“拒绝访问路径'C:\ temp.csv'。”如果我使用System.IO.File.Delete,则会出现相同的错误(请参阅末尾的代码部分以获取异常详细信息)。
奇怪的是,我可以手动删除文件,没有任何问题。另外,如果我复制文件我可以杀死副本,但仍然不是原始文件。我不相信这是一个连接/释放问题,因为我也无法杀死几天下载的文件。几台电脑重启前。我知道这不是目录权限问题,因为我可以手动将文件移动到桌面,但仍然无法通过代码将其删除。
我发现了一些具有相同问题的论坛帖子,但没有发布的答案。只是为了澄清,我不是试图从ftp站点删除文件,而是在我的机器上删除下载的文件。
我正在使用Visual Studio 2008.提前感谢!
例外细节:
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:
答案 0 :(得分:0)
使用AppData文件夹,问题应该消失。您的应用不会自动拥有所有文件夹中所有文件的所有权。
mUserFileDir = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & _
YourFolderName & YourFileName
或Environment.SpecialFolder.LocalApplicationData
取决于它在做什么以及为谁做。
也可以检查文件属性和持久性。可能是那里的东西。