C# - Delete user.config

By , 7/30/2013
(0 ratings)
Some uninstalling applications do not delete the user.config file and some created folders. For a "clean" uninstalling this should not happen. Following snippets shows how to avoid this problem.

The "normal" application is being extended to accept the argument "-del" – is this the case, the application starts, deletes the user.config and ends itself. Without any arguments, the program starts normally. So the same application is being used for "normal" execution as for deleting the user.config, what avoids deleting the wrong user.config.

Author: Günther Foidl, translation by Michael List
using System;
using System.Configuration;
using System.IO;

namespace TestSettings
{
	class Program
	{
		/// <param name="args">
		/// Command line arguments.
		/// <para>
		/// Values:
		/// <list type="table">
		///		<listheader>
		///			<term>Argument</term>
		///			<description>Command line argument</description>
		///		</listheader>
		///		<item>
		///			<term><c>-del</c></term>
		///			<description>deletes user.config</description>
		///		</item>
		/// </list>
		/// </para>
		/// </param>
		static void Main(string[] args)
		{
			if (args.Length > 0 && args[0] == "-del")
			{
				// Get user.config:
				Configuration config =
					ConfigurationManager.OpenExeConfiguration(
						ConfigurationUserLevel.PerUserRoamingAndLocal);

				// Determine path:
				DirectoryInfo di = new DirectoryInfo(config.FilePath);

				// Delete folder and parent folder of user.config
				// -> true enables deleting all files.
				di.Parent.Parent.Delete(true);

				// Leave program:
				return;
			}

			Console.WriteLine("Normal execution of application");
			Console.ReadKey();
		}
	}
}

Comments

 

Log in, to comment!