Last active
February 14, 2018 22:00
-
-
Save kmorin/650f78a2916a94b62a50397641ff9dc5 to your computer and use it in GitHub Desktop.
read and parse + update RevitINI files from custom actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ReadWriteRevitIniPaths() { | |
var iniLocations = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Autodesk", "Revit"); | |
foreach (var dir in Directory.GetDirectories(iniLocations)) { | |
if (dir.Contains("Addins")) { continue; } | |
foreach (var file in Directory.GetFiles(dir)) { | |
if (!Path.GetFileName(file).Equals("Revit.ini")) { continue; } | |
var contents = File.ReadAllLines(file); | |
//setup dictionary | |
var dict = new Dictionary<string, List<string>>(); | |
var key = ""; | |
foreach (var line in contents) { | |
if (string.IsNullOrEmpty(line)) { continue; } | |
if (line.StartsWith("[")) { | |
//this is a key | |
key = line; | |
dict.Add(key, new List<string>()); | |
continue; | |
} | |
dict[key].Add(line); | |
} | |
var directories = dict["[Directories]"]; | |
var match = "AdditionalRenderAppearancePaths"; | |
var renderPaths = directories.FirstOrDefault(x => x.StartsWith(match)); | |
var userDuluxDataPath = $@"%APPDATA%\Dulux"; | |
if (renderPaths != null) { | |
if (!renderPaths.Contains(userDuluxDataPath)) { | |
dict["[Directories]"].Remove(renderPaths); | |
var parsed = renderPaths + $@"|{userDuluxDataPath}"; | |
dict["[Directories]"].Add(parsed); | |
} | |
} | |
else { | |
//add the entries | |
dict["[Directories]"].Add($"{match}={userDuluxDataPath}"); | |
} | |
//Write out file | |
var newContents = new List<string>(); | |
foreach (var pair in dict) { | |
newContents.Add(pair.Key); | |
pair.Value.ForEach(x => newContents.Add(x)); | |
} | |
try { File.WriteAllLines(file, newContents); } | |
catch { } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment