Quantcast
Channel: WebForms – dougv.com « Doug Vanderweide
Viewing all articles
Browse latest Browse all 10

Retaining The LastWriteTime Value For A File About To Be Overwritten Via FTP / FileUpload By Using ASP.NET’s FileInfo Class

$
0
0

Recently asked on Yahoo! Answers:

Preserving last modified date when uploading (asp.net)?

I need to preserve (or change) the last modified date in the file headers when uploading via ftp. When I upload a file via ftp, the last modified date will be the date of upload. Creation date would be fine too. I just need to have the original date attached somewhere in the attributes.

Does anyone have a creative solution. For instance, can I create a container with custom attributes and then stream the data into it? If so how would I do it? Links and/or code would be greatly appreciated. Also, I am using vb.net but C# is fine too.

Keep in mind I must pass credentials to the remote server.

This is actually fairly straightforward, thanks to the FileInfo class in .NET — it’s a subclass of the FileSystemInfo class, which is a replacement of the FileSystemObject of yore.

And much like the old FileSystemObject class, FileInfo not only allows us to do some basic things with files — open, close, read, write and delete, for example — it also allows us to get or set certain properties within a file, including LastWriteTime. We’ll exploit that to achieve what this questioner wants.

FileInfo is part of the System.IO namespace, so if your page doesn’t already make use of System.IO, you’ll need to add it to your web.config pages namespaces, or to the page via the @Import directive.

I don’t know how this questioner’s FTP code works, but that shouldn’t really matter too much; the code can be easily customized to fit into the FTP class without having to alter it a bit. To demo the relevant code, I’ll be using a simple FileUpload class to upload a file to the server.

First, I need to declare the subroutine that will handle my file upload event; this questioner should already have a subroutine that is handling each file being sent via FTP.

As always, we begin by declaring some variables. In this case, we want to set some path variables: The physical path to the directory where the file will be uploaded; the name of the file being uploaded; and a string that will concatenate those are up first.

Sub UploadFile(ByVal Sender As Object, ByVal E As EventArgs)
        Dim strBasePath As String = "c:\path\to\upload\directory\"
        Dim strFileName As String
        Dim strFullPath As String

Probably this questioner’s FTP class has a property that contains the original name of the file being uploaded; that’s where she would get the strFileName value. The strBasePath value will need to be repeated here, if the FTP class doesn’t contain a property that exposes it; otherwise, you can get that from the FTP class, too.

Next, for the purposes of this demo, I’m going to echo out certain information about the uploaded file, to prove the code works. You can eliminate this from your script.

Dim strStatus As String

We next declare a FileInfo variable, but don’t run it through the constructor, which would require us to give the full file path. The reason for that is twofold: First, we don’t yet have the name of the file being uploaded; but more important, we may want to make this subroutine recursive, so using the constructor at this stage would prevent us from doing that.

We’re also going to declare a DateTime variable that will hold the last write time we will get from the FileInfo class, and set its default value to be the current time.

Dim fiCurrent As FileInfo
Dim dtModTime As DateTime = Now

My status label is updated with the value I just assigned to dtModTime. Again, you can delete this safely.

strStatus = "The system date and time is " + dtModTime.ToString("F") + "<br />" + vbCrLf

Here is where the questioner would begin modifying the code. In my demo, I check to see if my upload control has a file; the questioner would instead modify whatever code she is using to check if there is a file about to be uploaded via FTP, or would put in code to recursively call this subroutine for each file that’s about to be uploaded.

Whichever the case, it is vital to call the following block of code before the file is actually sent to the destination directory; otherwise, we won’t know if the file already exists in the directory.

The block below sets the strFileName variable to be the same name as the file that is about to be saved in the destination directory; it then appends that name to the strBasePath variable, and creates a new FileInfo object from that path, using the fiCurrent name we previously declared.

We then check to see if the specified file exists in the destination directory.

If it does, we then set the dtModTime variable we declared earlier to be the same date and time as the LastWriteTime property of the file that is in the destination directory.

If the file does not exist in the destination directory, we do nothing; we’ve already set the default time for dtModTime to be Now.

Note that again, I am appending text to my status message; again, you can delete this safely from the code.

If fuUpload.HasFile Then
            strFileName = fuUpload.FileName

            strStatus += "The name of the file being uploaded is " + strFileName + vbCrLf + "<br />" + vbCrLf

            strFullPath = strBasePath + strFileName
            fiCurrent = New FileInfo(strFullPath)

            If fiCurrent.Exists Then
                strStatus += "The file currently exists in the upload directory."

                dtModTime = fiCurrent.LastWriteTime
            Else
                strStatus += "The file does not currently exist in the upload directory."
            End If

            strStatus += "<br />" + vbCrLf

Now that we have checked to see if the uploaded file is already in our destination directory, and saved the date and time in our dtModTime variable, we can save the uploaded file to the destination directory. Again, the questioner will have to mod her code to do this with her FTP class.

fuUpload.SaveAs(strFullPath)

The last part of this script does the magic. We simply set the LastWriteTime of the newly uploaded file to be whatever value we stored in dtModTime.

fiCurrent.LastWriteTime = dtModTime

In my demo, I want to report the LastWriteTime value of the uploaded file; to do that, I refresh the fiCurrent class, then write out the LastWriteTime for the file. Again, you can safely delete this from your code.

fiCurrent.Refresh()
strStatus += "The last write time for the uploaded file is " + fiCurrent.LastWriteTime.ToString("F") + "<br /><br />" + vbCrLf
lblStatus.Text = strStatus

And yes, that’s all there is to it.

Because this involves file uploading, and file uploading is dangerous, I won’t provide a working demo of this code; I have tested it, however, and it does work just fine.


Viewing all articles
Browse latest Browse all 10

Trending Articles