I’m working on some code that deals with parsing files (mainly XML, but there are some custom file types in there, too). I’ve got a few try-catches around the parsing code, on the off chance that something goes wrong when loading or accessing the files, and these exceptions are thrown back up to the calling object/methods.
Here’s an example:
/* main.cs */
public void LoadSomeFile (string _fullPathToFile)
{
//create an instance of my fileLoader object
//which is just a wrapper for the in-built
//c# and .net methods for loading a file
FileLoader myFileLoader = new FileLoader();
try
{
//pass the fileLoader the location the file
//to be loaded
myfileLoader.LoadAFile(_directoryAndFileName);
}
catch (ParsingException parseEx)
{
//code to create or append to debug log file
}
//once the file has been loaded and parsed,
//or if it fails, close the fileLoader
myFileLoader.Close();
}
/* ParsingException class */
public class ParsingException : System.Exception
{
public ParsingException()
{
}
public ParsingException(string message, Exception innerException)
{
}
public ParsingException(string message)
: base(message)
{
}
/* in FileLoader.cs */
public void LoadAFile(string _fullPathToFile)
{
try
{
/* load the file from disk */
}
catch (FileLoadException fileLoadEx)
{
throw new ParsingException("Could not load file.\nDetails: "
+ fileLoadEx.Message);
}
catch (FileNotFoundexception fileNotFoundEx)
{
throw new ParsingException("Could not find file.\nDetails: "
+ fileNotFoundEx.Message);
}
catch (NullReferenceException nullRefEx)
{
throw new ParsingException("Could not load data from file.\nDetails: "
+ nullRefEx.Message);
}
}
I’m looking to output the parsing exceptions (and any other exceptions thrown for that matter) to a log file (the filename and format don’t matter for now, but I’m thinking of a simple text file with the name debug.txt). However, since this piece of software might be loaded onto different versions of Windows (or even different environments through Mono and similar tools/VM), I’m concerned about where I can place this debug file.
I’m building on Windows 7 Professional with UAC turned off, but if this software is loaded onto a machine with UAC turned on (either on XP, Vista or 7) I will be severely limited in my choice of possible output directories of this debug file.
Are there any specific directories that my software will definitely be able to output to, regardless of OS version and UAC settings? I was thinking that the software’s startup path should be ok (Program Files\My Project\ or Program Files (x86)\My Project\ for example ), but will there be a circumstances when this is inaccessible? (Aside from if the startup directory is made Read Only, of course) Or are there any directories that might prove better for this sort of thing? I’m hoping to find a directory that the user can navigate to easily.
With UAC on Program Files is not on.
Look at Environment.SpecialFolders. LocalApplicationData is a reasonable choice
Standard trick is to Create a folder for you company under it, then one under that for your app, and then put your files in there.
Known location and UAC won’t affect it.