JonnyT
12-04-2009, 01:33 PM
Hi. I'm fairly new at programming, and I'm quite pleased at the results I have made so far. However, I need help with something that you may consider quite simple. I need some data to come out of a void into the Main void to be used later. The data is gathered from the latest .txt file in a set directory. Here is what I have so far...
private static void RecentTXTfile()
{
// MOST RECENT TXT FILE
try
{
Console.WriteLine("Going to look for file in this directory...");
string lDir = (@"\my\directory");
string rootDir = @lDir;
IEnumerable<System.IO.FileInfo> fileList = GetAllFiles(rootDir);
Console.WriteLine("{0} file(s) found on {1}", fileList.Count(), lDir);
Console.WriteLine("Extension to browse for...");
string lExt = ".txt";
//run a query for by extension using LINQ
//list all files order by filename
IEnumerable<System.IO.FileInfo> fileQuery =
from file in fileList
where file.Extension.ToLower() == lExt.ToLower()
orderby file.Name
select file;
foreach (FileInfo fi in fileQuery)
{
Console.WriteLine(fi.FullName);
}
// Get the latest/newest file
var latestFile =
(from file in fileQuery
orderby file.CreationTime
select new { file.FullName, file.CreationTime })
.Last();
Console.WriteLine("\r\nThe latest file is {0}. Creation time: {1}",
latestFile.FullName, latestFile.CreationTime);
string newestfile = System.IO.File.ReadAllText(latestFile.FullName);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
}
}
// retrieve all files on the specified directory
static IEnumerable<System.IO.FileInfo> GetAllFiles(string path)
{
if (!System.IO.Directory.Exists(path))
throw new System.IO.DirectoryNotFoundException();
string[] fileNames = null;
List<System.IO.FileInfo> files = new List<System.IO.FileInfo>();
fileNames = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
foreach (string name in fileNames)
{
files.Add(new System.IO.FileInfo(name));
}
return files;
}
then I want the string "newestfile" to copy it's contents into a different text file further down my code.
// MOST RECENT FILE CONTENTS
SW.WriteLine(newestfile);
I'm not entirely sure how to do this, as I keep getting the error saying that the name "newestfile" does not appear in the current context.
Any help is appreciated :)
private static void RecentTXTfile()
{
// MOST RECENT TXT FILE
try
{
Console.WriteLine("Going to look for file in this directory...");
string lDir = (@"\my\directory");
string rootDir = @lDir;
IEnumerable<System.IO.FileInfo> fileList = GetAllFiles(rootDir);
Console.WriteLine("{0} file(s) found on {1}", fileList.Count(), lDir);
Console.WriteLine("Extension to browse for...");
string lExt = ".txt";
//run a query for by extension using LINQ
//list all files order by filename
IEnumerable<System.IO.FileInfo> fileQuery =
from file in fileList
where file.Extension.ToLower() == lExt.ToLower()
orderby file.Name
select file;
foreach (FileInfo fi in fileQuery)
{
Console.WriteLine(fi.FullName);
}
// Get the latest/newest file
var latestFile =
(from file in fileQuery
orderby file.CreationTime
select new { file.FullName, file.CreationTime })
.Last();
Console.WriteLine("\r\nThe latest file is {0}. Creation time: {1}",
latestFile.FullName, latestFile.CreationTime);
string newestfile = System.IO.File.ReadAllText(latestFile.FullName);
// Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.Read();
}
}
// retrieve all files on the specified directory
static IEnumerable<System.IO.FileInfo> GetAllFiles(string path)
{
if (!System.IO.Directory.Exists(path))
throw new System.IO.DirectoryNotFoundException();
string[] fileNames = null;
List<System.IO.FileInfo> files = new List<System.IO.FileInfo>();
fileNames = System.IO.Directory.GetFiles(path, "*.*", System.IO.SearchOption.AllDirectories);
foreach (string name in fileNames)
{
files.Add(new System.IO.FileInfo(name));
}
return files;
}
then I want the string "newestfile" to copy it's contents into a different text file further down my code.
// MOST RECENT FILE CONTENTS
SW.WriteLine(newestfile);
I'm not entirely sure how to do this, as I keep getting the error saying that the name "newestfile" does not appear in the current context.
Any help is appreciated :)