Note that there are some explanatory texts on larger screens.

plurals
  1. PODirectory.GetFiles keeping the last access time
    text
    copied!<p>It appears that Directory.GetFiles() in C# modifies the Last access date of a file. I've googled for hours and can't seem to find a work around for this issue. Is there anyway to keep all the MAC (Modified, Accessed, Created) attributes of a file? I'm using Directory.GetDirectories(), Directory.GetFiles(), and FileInfo.</p> <p>Also, the fi.LastAccessTime is giving strange results -- the date is correct, however, the time is off by 2 minutes, or a few hours.</p> <pre><code>Time of function execution: 10/31/2008 8:35 AM Program Shows As Last Access Time 0_PDFIndex.html - 10/31/2008 8:17:24 AM AdvancedArithmetic.pdf - 10/31/2008 8:31:05 AM AdvancedControlStructures.pdf - 10/30/2008 1:18:00 PM AoAIX.pdf - 10/30/2008 1:18:00 PM AoATOC.pdf - 10/30/2008 12:29:51 PM AoATOC2.pdf - 10/30/2008 1:18:00 PM Actual Last Access Time 0_PDFIndex.html - 10/31/2008 8:17 AM AdvancedArithmetic.pdf - 10/30/2008 12:29 PM AdvancedControlStructures.pdf - 10/30/2008 12:29 PM AoAIX.pdf - 10/30/2008 12:29 PM AoATOC.pdf - 10/30/2008 12:29 PM AoATOC2.pdf - 10/30/2008 12:29 PM </code></pre> <p>Below is the method I'm using. If you require more information, please let me know.</p> <p>Thanks!</p> <pre><code>public void PopulateTreeView(string directoryValue, ref TreeNode parentNode) { string[] directoryArray = Directory.GetDirectories(directoryValue); string[] fileArray = Directory.GetFiles(directoryValue, "*.*", SearchOption.AllDirectories); try { #region Directories if (directoryArray.Length != 0) { foreach (string directory in directoryArray) { DirectoryInfo di = new DirectoryInfo(directory); TreeNode dirNode = parentNode.Nodes.Add(di.Name); FileNode fn = new FileNode(); fn.bIsDir = true; fn.dir = di; dirNode.Tag = fn; PopulateTreeView(directory, ref dirNode); Application.DoEvents(); } } #endregion #region Files if (fileArray.Length != 0) { foreach (string file in fileArray) { FileInfo fi = new FileInfo(file); TreeNode fileNode = parentNode.Nodes.Add(fi.Name); FileNode fn = new FileNode(); fn.bIsDir = false; fn.file = fi; fileNode.Tag = fn; fileNode.ImageIndex = 1; Console.WriteLine(fi.Name + " - " + fi.LastAccessTime); } } #endregion } catch (UnauthorizedAccessException) { parentNode.Nodes.Add("Access denied"); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } finally { Application.DoEvents(); } } </code></pre> <hr> <p>i know the differences between the attributes. What i need is for the file to remain exactly the same all attributes and meta-data, as if my program never touched the file; this includes the last access date.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload