Link to home
Start Free TrialLog in
Avatar of kmcbrearty
kmcbreartyFlag for United States of America

asked on

FTP Files to directory in inetpub

I have some FTP code that I have been using for a while now.  Recently I have been testing the code using Windows vista.  The code downloads files from an FTP site overwriting old files in a web application in inetpub.  The code still works fine provided that the file that is being downloaded doesn't exist.

I tried to delete the file before it is downloaded using file.delete(path) so it wouldn't exist when it was downloaded.  It doesn't look like vista allowed me to delete the file.

The majority of the code that I am using came from http://www.codeproject.com/KB/IP/FtpClient.aspx.  I have made a few modifications to the code to suit my needs such as making it configurable as to whether or not it uses active or passive ftp mode.

Any ideas as to why this isn't working and/or how to get it to work?
Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

That sounds like a security issue.  Check the FTP directory and make sure the built in IUSR account has read/write/modify permissions.
Actually, if you are using anonymous FTP, it looks like it uses the IUSR_MachineName user.
It does sound like authorisation issue. Catch the exception (IOException) and read the stack trace. It should tell you if access is denied.
Avatar of kmcbrearty

ASKER

I don't think it is a security issue.  The application that is updating the Web Application is a Windows Forms application.  I am executing the application manually so everything should run under the context of my user.  I can create, edit, and modify file in the directory in question without any problems.  As mentioned previously for some reason with Windows Vista the FTP code can not delete and/or modify files in the inetpub directory.
Are you able to post the IOException?

Also if you running in vista, compile a version of your application right click on the application and click the option "run as administrator". Especially if you have UAC enabled. Vista by default will not run applications with admin permissions.
http://www.jimmah.com/vista/security/uac.aspx
There is no exception being thrown.  I stepped through the attached code snippet and at no time was there an exception thrown for the file that I am monitoring.  Also, I have the UAC turned off but I did try to run the application as administrator but I had the same result.  As far as the program is concerned everything ran fine but the file was not overwritten.
            'open request and get response stream
            Using response As FtpWebResponse = CType(ftp.GetResponse, FtpWebResponse)
                Using responseStream As Stream = response.GetResponseStream
                    'loop to read & write to file
                    Using fs As FileStream = targetFI.OpenWrite
                        Try
                            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()
                        Catch ex As Exception
                            'catch error and delete file only partially downloaded
                            fs.Close()
                            'delete target file as it's incomplete
                            targetFI.Delete()
                            Throw ex
                        End Try
                    End Using
                    responseStream.Close()
                End Using
                response.Close()
            End Using

Open in new window

What error do you get when you use IO.File.Delete() on the file that exists?  Or does it just not do anything?
I don't get any error messages at all.  That is what is so frustrating about this whole situation.  No matter what I try it dosn't do what it is supposed to nor does it indicate that there is a problem.
ASKER CERTIFIED SOLUTION
Avatar of aibusinesssolutions
aibusinesssolutions
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, it just got more weird.  After running the attached code snippet and stepping through the code.  It sends the file to the recycle bin but the file is never deleted.  As before, no exceptions are thrown.
' For Some Reason it is not overwriting existing files on some versions of Windows
' Delete the file before downloading it
If File.Exists(FileDestination) Then
    'File.Delete(FileDestination)
    My.Computer.FileSystem.DeleteFile(FileDestination, FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
End If

Open in new window

Are you sure it's not deleting the file and writing a new one?  
I can not say that for sure.  Either way the file is not removed from the inetpub directory.
Check the date modified on the file before and after you delete it, see if it is different.  If the file showed up in the recycle bin, then it is being deleted, otherwise it would throw an error.  It sounds like something else is causing a new file to be created.
Ok, I feel dumb.  Your solution was working.  I had to connect to another one of our servers that has identical files earlier today and was looking at the files on that machine rather than my own.  I am now able to delete the file if it exists and then download the new correct file.  

Thank you for your help.
Just to let you know your solution wasn't the exact problem but it did lead me to it.  Either way without your post I would have found the problem.  The real problem was with my test to determine whether or not the file exists.  Instead of using File.Exists which is the function from System.IO I used FileExists which is a custom function that I wrote to determine if the file exists on the FTPSite.  The location that I was passing to this function wouldn't have existed on the FTP site.  When I posted the code for you I found my problem.  Then I just had to realize what machine I was looking at.

Anyway thanks again, and thank you for your patience.