Simon Harriyott

Hiding configuration files

When writing a reusable, configurable .NET application, there are often a load of text files that need deploying. To prevent users tampering with the configuration files that control the behaviour of the application, the files can be embedded as a resource. This is particularly useful for XML schemas (why does nobody ever use the original plural, schemata? Oh yeah, it sounds poncy. Still, we do say data instead of datums). The contents of the file can be read using reflection:

public string GetFileContents(string resName)
{
string contents = "";

Assembly thisAssembly = Assembly.GetCallingAssembly();

// Read the file from the resource stream
Stream resStream
= thisAssembly.GetManifestResourceStream(resName);
if (resStream != null)
{
StreamReader reader = new StreamReader(resStream);
contents = reader.ReadToEnd();
}
return contents;
}


The advantage is that the text files aren't even visible to the user, let alone tamperable. The disadvantages are that they are read only, so not appropriate for user settings, and that any changes require a rebuild and deployement. Therefore, appropriate in some cases, but not in others.
26 August 2005