Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 647
  • Last Modified:

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?
0
kmcbrearty
Asked:
kmcbrearty
  • 7
  • 6
  • 2
1 Solution
 
aibusinesssolutionsCommented:
That sounds like a security issue.  Check the FTP directory and make sure the built in IUSR account has read/write/modify permissions.
0
 
aibusinesssolutionsCommented:
Actually, if you are using anonymous FTP, it looks like it uses the IUSR_MachineName user.
0
 
theplonkCommented:
It does sound like authorisation issue. Catch the exception (IOException) and read the stack trace. It should tell you if access is denied.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kmcbreartyAuthor Commented:
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.
0
 
theplonkCommented:
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
0
 
kmcbreartyAuthor Commented:
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

0
 
aibusinesssolutionsCommented:
What error do you get when you use IO.File.Delete() on the file that exists?  Or does it just not do anything?
0
 
kmcbreartyAuthor Commented:
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.
0
 
aibusinesssolutionsCommented:
Try this:

My.Computer.FileSystem.DeleteFile("path-to-file", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
0
 
kmcbreartyAuthor Commented:
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

0
 
aibusinesssolutionsCommented:
Are you sure it's not deleting the file and writing a new one?  
0
 
kmcbreartyAuthor Commented:
I can not say that for sure.  Either way the file is not removed from the inetpub directory.
0
 
aibusinesssolutionsCommented:
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.
0
 
kmcbreartyAuthor Commented:
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.
0
 
kmcbreartyAuthor Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now