How to get LastAccessTime of a file without modifying it?

G

Guest

Hi,

i am reading the LastAccessTime, LastWriteTime and DateofCreation of a file.
The second and third are working well, but each time i want to read the
LastAccessTime, its set to "now". How can i read the LastAccessTime of a file
without modifying it?

thanks
Yavuz Bogazci
 
N

Nick Malik [Microsoft]

post code, please.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
G

Guest

Here is my Code:

Dim MyDir As New DirectoryInfo(Dir)

For Each File As FileInfo In MyDir.GetFiles
file.GetLastAccessTime
Next

Not more.

Thanks
Yavuz Bogazci
 
N

Nick Malik [Microsoft]

a rather unusual and not useful bit of code. I assume that you extracted
this from your app. Have you checked to make sure that THIS code still
exhibits the error in question?

I will send this code to myself (my other PC has Visual Studio) and see if I
can recreate your problem... just for kicks.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
N

Nick Malik [Microsoft]

OK, So I took the code you posted, dropped into a button click event on a
brand new form, and... it didn't compile.
After a quick touch-up, I compiled it and it ran... and no error. The
LastAccessTime property worked properly and did not return today's date on
every file.

I do not doubt that you are getting the error you say you are. However, I
suspect that if you look more carefully at your code, you are accessing the
file before you call LastAccessTime, and that your own code is resetting the
access date.

Anyway, here is the code that works, and my notes:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim MyDir As New DirectoryInfo("C:\Downloads")
Dim Dt As Date
For Each File1 As FileInfo In MyDir.GetFiles
Dt = File1.LastAccessTime
MessageBox.Show("File: " + File1.Name + " Last Access " +
Dt.ToString())
Next
End Sub

Notes:

There is no GetLastAccessTime method on the FileInfo class. I assume that
you meant to use the LastAccessTime property.
You cannot use the variable name File in your For Each stmt because that is
also the name of a static class. If you use File1, you don't get an error.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik


Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top