Delete entries in [Recent File List] in Revit.ini file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have a plug-in that has to open a lot of revit files from a specific folder to execute some actions before closing them again, which is causing my Recent File List to become poluted with all the files that were opened.
Ideally, I would like to go through the Revit.ini file, navigate to the [Recent File List] and delete all the files opened from that specific folder (ResourcesFolder). Here 's a working script I did to modify the Recent File List in the Revit.ini file:
void ClearRecentFiles()
{
string runningRevitVersion = thisControlApplication.ControlledApplication.VersionNumber;
string iniFile = $"{Environment.GetEnvironmentVariable("appdata")}\\Autodesk\\Revit\\Autodesk Revit {runningRevitVersion}\\Revit.ini";
string tempFilePath = Path.GetTempFileName();
if (File.Exists(iniFile))
{
using (var reader = new StreamReader(iniFile, Encoding.Unicode))
using (var writer = new StreamWriter(tempFilePath, false, Encoding.Unicode))
{
bool inRecentFileListSection = false;
string inputLine = "";
int fileIndex = 1;
while ((inputLine = reader.ReadLine()) != null)
{
if (inputLine.Trim().Equals("[Recent File List]", StringComparison.OrdinalIgnoreCase))
{
inRecentFileListSection = true;
writer.WriteLine(inputLine);
continue;
}
if (inRecentFileListSection == true)
{
if (inputLine.StartsWith("[") && inputLine.EndsWith("]"))
{
inRecentFileListSection = false;
}
else if (inputLine.Contains(ResourcesFolder))
{
continue;
}
else
{
var split = inputLine.Split(new char[] { '=' });
inputLine = $"File{fileIndex}={split[1]}";
fileIndex++;
}
}
writer.WriteLine(inputLine);
}
}
try
{
File.Replace(tempFilePath, iniFile, null);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
However, it's come to my understanding that the Revit.ini file is only written/overwritten after OnShutdown() method is run and that I actually need to close Revit before modifying the Revit.ini for this to work.
I could maybe create a standalone application that is triggered each time revit is closed to clear the Recent files list but is that approach really the only/best approach? This would also not solve the problem that if I close all my tabs (Documents) in Revit without closing Revit, I would still get the poluted Recent Files list because the script is only run after Revit is closed.
Any suggestions?
Best Regards,
Sofia