Recently Modified File

  • Thread starter Thread starter Dyl
  • Start date Start date
D

Dyl

Hello,

I'm working on trying to find the most recently modified file, but I
keep coming across an error when building.

Here's my code:

DirectoryInfo Direc = new DirectoryInfo(@"U:\xxxx\xxxx");

FileInfo[] FoldersArr = Direc.GetFiles();

if (FoldersArr.Length > 0)
{
DateTime MostRecent = FoldersArr[1].LastWriteTime;
FileInfo RecentFolder;

foreach (FileInfo f in FoldersArr)
{
if (MostRecent.CompareTo(f.LastWriteTime) < 0)
{
MostRecent = f.LastWriteTime;
RecentFolder = f;
}
}
}

MessageBox.Show(RecentFolder.ToString());


Error Message: The type or namespace name 'RecentFolder' could not be
found (are you missing a using directive or an assembly reference?)

Any help would be appreciated as always. Thanks to whoever takes the
time to answer these posts.
 
Dyl said:
Hello,

I'm working on trying to find the most recently modified file, but I
keep coming across an error when building.

Here's my code:

DirectoryInfo Direc = new DirectoryInfo(@"U:\xxxx\xxxx");

FileInfo[] FoldersArr = Direc.GetFiles();

if (FoldersArr.Length > 0)
{
DateTime MostRecent = FoldersArr[1].LastWriteTime;
FileInfo RecentFolder;

foreach (FileInfo f in FoldersArr)
{
if (MostRecent.CompareTo(f.LastWriteTime) < 0)
{
MostRecent = f.LastWriteTime;
RecentFolder = f;
}
}
}

MessageBox.Show(RecentFolder.ToString());

Error Message: The type or namespace name 'RecentFolder' could not be
found (are you missing a using directive or an assembly reference?)

The definition of RecentFolder is nested within your if statement. As a
result, by the time you get to your MessageBox statement that variable
has gone out of scope and is no longer usable. Either move the
MessageBox inside of the if statement, or declare RecentFolder and
initialize it outside of your if block.
 

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

Back
Top