PDA

View Full Version : Resolved C# Help (Getting Data from void to Main void)


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 :)

nobodytold
12-04-2009, 03:14 PM
Hey there. That seems like some pretty good stuff for someone just getting started.

So, I'm not entirely sure what you want to do. I assume you mean "main" to be the main running loop, which in fact is usually not void but returns an int.

int main (int argument_count, const char * argv[])
{
// Do some stuff here

return 0 ;
}


So I'm not sure if you are aware, but functions can have a return type other than void (as shown above). Also, if you meant you wanted to keep the function void and still have another way of "keeping" the data. I guess what you could do is pass in a pointer, and inside the function that loads your text file, just set that equal to the data you read from the file.


The error you having is probably a scoping issue. Basically, that variable you declared inside RecentTXTfile won't be accessible anywhere else.


You might consider posting more of your program... so it becomes clearer what you are trying to do.

JonnyT
12-04-2009, 03:48 PM
Basically the first block of code finds the most recent text file in a given directory and copies it's contents to a string. I then want the contents of the string to be inserted into a different text file further down in the code outside of that first block.

By using this code for example: SW.WriteLine(newestfile);

In other words, how can I make the "newestfile" string accessible outside of that block of code? I know about changing the void to something else, but what can I change it to and still make it work?

oracleguy
12-04-2009, 04:26 PM
private static string RecentTXTfile()
{
string newestfile = String.Empty;
// 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);

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();
}

return newestfile;
}


You can return a string from that function. Then in your main:


static void main()
{
Console.Writeline(RecentTXTfile());
}


Does that make sense?

JonnyT
12-04-2009, 05:29 PM
Hi. Thanks for your successful solution above :) It does exactly what I want it to do and I understand what you did to make it work ;)