Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
private void SaveRecentFile(string path)
{
recentToolStripMenuItem.DropDownItems.Clear(); //clear all recent list from menu
LoadRecentList(); //load list from file
if (!(MRUlist.Contains(path))) //prevent duplication on recent list
MRUlist.Enqueue(path); //insert given path into list
while (MRUlist.Count > MRUnumber ) //keep list number not exceeded given value
{
MRUlist.Dequeue();
}
foreach (string item in MRUlist )
{
ToolStripMenuItem fileRecent = new ToolStripMenuItem(item, null, RecentFile_click); //create new menu for each item in list
recentToolStripMenuItem.DropDownItems.Add(fileRecent); //add the menu to "recent" menu
}
//writing menu list to file
StreamWriter stringToWrite = new StreamWriter(System.Environment.CurrentDirectory + "\\Recent.txt"); //create file called "Recent.txt" located on app folder
foreach (string item in MRUlist )
{
stringToWrite.WriteLine(item); //write list to stream
}
stringToWrite.Flush(); //write stream to file
stringToWrite.Close(); //close the stream and reclaim memory
}

What I have tried:

i already tried to make like a recent open file but its no good it not working in my c# program. please somebody who knows th code.
Posted
Updated 24-May-16 20:45pm
Comments
koolprasad2003 25-May-16 2:13am    
Do you want to know get the list of recent opened .txt files ?

1 solution

Don't do it like that: all you end up with is a string you can't use:
d:\temp\myfile.txtc:\folder\myotehrfile.txt

Instead, use the String.Join method:
C#
string toSave = string.Join("|", MRUList);
File.WriteAllText(pathToFile, toSave);

Then to read them back:
C#
string rawData = File.ReadAllText(pathToFile);
string[] individualFiles = rawData.Split('|');
Since the '|' character can;t appear in any Windows file or folder name it's safe to use as a separator.

And BTW: don't store data in the application folder. It works in development, but tends to fail in production. See here: Where should I store my data?[^] it shows some better places and how to use them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900