Delete entries in [Recent File List] in Revit.ini file

Delete entries in [Recent File List] in Revit.ini file

sofia_feist
Enthusiast Enthusiast
600 Views
3 Replies
Message 1 of 4

Delete entries in [Recent File List] in Revit.ini file

sofia_feist
Enthusiast
Enthusiast

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

0 Likes
601 Views
3 Replies
Replies (3)
Message 2 of 4

jeremy_tammik
Alumni
Alumni

Yes, I have a suggestion. Hopefully, it avoids all your problems in a simple way. My suggestion is: 

  

  • Do not write anything to Revit.ini at all
  • Before opening any of the polluting files, read the recent file list from Revit.ini and store it in a variable A
  • After opening the files and thus polluting it, open (and close) all of the files in A again

  

Wouldn't that solve your problem in a simple manner?

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 4

sofia_feist
Enthusiast
Enthusiast

It is a viable suggestion but I wanted to avoid even more waiting time of an already lengthy operation, especially when my Recent File List has 28 files to open and close in it.

0 Likes
Message 4 of 4

jeremy_tammik
Alumni
Alumni

My assumption was that the process opening and closing such a large number is set up to run unattended, with nobody sitting and waiting for it to end. Good luck finding an optimal solution! Please let us know what that is for you. Thank you!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open