How to restore Configuration Settings when they fail to initialize - c#

I have my program which has some Application Settings and some User Settings. On some machines my User Settings config file becomes corrupt and stops my program loading. I then log into the machine and delete the UserConfig directory stored at
%USERPROFILE%\Appdata\Local\MyApp
When my file is corrupt the error thrown is Configuration Settings Failed To Initialize so I was wondering if this happened in my program whether there was a way to delete the corrupt file and reset the configuration.
So far I have:
try
{
var prop1= Settings.Default.prop1;
}
catch (Exception ex)
{
var userSettingsLocation =
Path.Combine(Environment.ExpandEnvironmentVariables(
"%USERPROFILE%"), "AppData","Local", "MyApp");
if (Directory.Exists(userSettingsLocation))
{
DeleteDirectory(userSettingsLocation); // This is a reccursive
// delete method
// I need to reload settings
}
}
This deletes the file fine but if I try to read my settings again using for example: Settings.Reset(); I still get the same error. I need to somehow reset the configuration Settings after deleting the corrupt file. Is this possible?

So heres the trick if anyone else wants to know:
try
{
var prop1= Settings.Default.prop1;
}
catch (Exception ex)
{
var userSettingsLocation =
Path.Combine(Environment.ExpandEnvironmentVariables(
"%USERPROFILE%"), "AppData","Local", "MyApp");
if (Directory.Exists(userSettingsLocation))
{
if (ex.InnerException is System.Configuration.ConfigurationException)
{
var settingsFile = (ex.InnerException as ConfigurationException).Filename;
File.Delete(settingsFile);
System.Windows.Forms.Application.Restart();
}
}
}

Edit: after some trials, I think you need to restart the application after deleting the faulty config file. Here another SO thread: C# - User Settings broken
The last answer there is essentially the code you could use.
Actually I think you must call Settings.Reset after deleting the file.
By the way you should use the exception details to get the config file name causing the issue:
catch(Exception ex)
{
if(ex.InnerException is ConfigurationErrorsException)
{
var settingsFile = (e.InnerException as ConfigurationErrorsException).Filename;
/* ....Your code... */
}
}
string filename (()e.InnerException).Filename;

Related

Read from an external text file in a built WPF application

I am building a desktop application in WPF and it requires user preferences from an external text file. The file should be available for the user to directly manipulate after final building and publishing.
String settingsPath = "settings.txt";
try
{
if (!File.Exists(settingsPath))
throw new Exception("settings file does not exist");
String settingsText = File.ReadAllText(settingsPath);
MessageBox.Show(settingsText);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Window.GetWindow(this).Close();
}
Even when I create a settings.txt file using file explorer, it throws the exception when built.
The text file you are creating is probably not in the working directory folder.
Use this code to diagnose your problem, it will show you where your code is looking for the file.
String settingsPath = "settings.txt";
try
{
if (!File.Exists(settingsPath))
throw new FileNotFoundException("settings file does not exist", Path.GetFullPath(settingsPath));
String settingsText = File.ReadAllText(settingsPath);
MessageBox.Show(settingsText);
}
catch (FileNotFoundException fileEx)
{
MessageBox.Show($"{fileEx.Message}:{fileEx.FileName}");
Window.GetWindow(this).Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Window.GetWindow(this).Close();
}

How to encrypt your Connection String in Advanced Installer (13.3) Custom Action

With Advanced Installer, I'm trying to make a Custom Action, that at installationtime, encrypt the Connection String.
I seems like I can't use "~" here. (I moved my working code from the MVC project, to here).
Is there a simple alternative to that line or am I forced to make a complete rewrite and use e.g. a solution that uses somekind of Stream (like this Modifying Web.Config During Installation
Exception thrown by custom action:
System.Reflection.TargetInvocationException: Exception has been thrown by the
target of an invocation. ---> System.ArgumentException:
The application relative virtual path '~' is not allowed here.
Custom Action:
[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
try
{
var config = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)config.GetSection("connectionStrings");
var cms = section.ConnectionStrings[GetConnectionStringName()];
var connStr = BuildConnStr(session["CONN_STR_SERVER"], session["CONN_STR_DATABASE"], session["CONN_STR_USERNAME"], session["CONN_STR_PASSWORD"]);
if (cms == null)
{
// Add new Connection String
section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr));
}
else
{
// Update existing Connection String
cms.ConnectionString = connStr;
}
// Encrypt
section.SectionInformation.ProtectSection(ConnStrEncryptionKey);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
return ActionResult.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.StackTrace, ex.Message);
throw;
}
}
The solution to the path issue, is to use ConfigurationManager a long with some mapping, like this, instead of the web version WebConfigurationManager.
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
The encryption works fine as the code is, but the issue with save is still not solved because the execution time is to early. The installation isn't finished and the web.config isn't yet copyed to the APPDIR.

How to empty contents of a log file being used by the same program

I have a C# application which uses log4net to write some log outputs in a file names "logfile.txt" residing in the application directory. I want to empty the contents of the file as soon as it reaches a size of 10GB.
For that I'm using a timer which keeps checking whether the size of the file crosses 10GB.
But I cannot perform any operation on "logfile.txt" since it is being used by other threads to write log outputs and it's throwing me,
System.IO.IOException "The process cannot access the file 'C:\Program Files\MyApps\TestApp1\logfile.txt' because it is being used by another process."
Here is the code of the timer which checks the size of the file "logfile.txt"
private void timer_file_size_check_Tick(object sender, EventArgs e)
{
try
{
string log_file_path = "C:\\Program Files\\MyApps\\TestApp1\\logfile.txt";
FileInfo f = new FileInfo(log_file_path);
bool ex;
long s1;
if (ex = f.Exists)
{
s1 = f.Length;
if (s1 > 10737418240)
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(log_file_path);
//File.Create(log_file_path).Close();
//File.Delete(log_file_path);
//var fs = new FileStream(log_file_path, FileMode.Truncate);
}
}
else
{
MDIParent.log.Error("Log file doesn't exists..");
}
}
catch (Exception er)
{
MDIParent.log.Error("Exceptipon :: " + er.ToString());
}
}
You shouldn't delete a log file on your own because log4net can do it for you. If you use RollingFileAppender you can specify the maximum file size (maximumFileSize property). Additionally if you set maxSizeRollBackups property to 0, then the log file will be truncated when it reaches the limit. Please look at this question for an example.

Trouble Moving files in C#?

I am making a software that will move files from the downloads folder to a specific sub folder in a directory. The sub folder is selected by the user by a combobox. I keep getting this error: System.IO.IOException: Cannot create a file when that file already exists. Also, these error come up on people's computer who install my program...exceptions and things. How do i turn it off. Also, why do i get this error? Here is my code:
string pathUser4 = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload4 = (pathUser4 + #"\Downloads\");
string sourceFile = pathDownload4 + listBox1.Text;
string pathdoc5 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string pathDownload5 = (pathdoc5 + #"\iracing\setups\");
string destinationFile = pathDownload5 + comboBox1.Text;
File.Move(sourceFile, destinationFile);
if (comboBox1.Text == "Select File Destination")
{
MessageBox.Show("Please Select A Destination Folder", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Each File.Move should be wrapped in a try/catch block as you can never expect an IO operation to execute without error. It could be something as simple as the user having a file handle open, or the file existing in the destination folder, either way, you don't want a single file to throw an exception that stops the entire operation. You will want to catch the exceptions and log them either to an error log file or to the event log, this way you can see the errors that occurred but it will not interrupt anything.
Secondly, for any desktop application I would add global error handling to log any uncaught errors. You can do this by putting this code at the beginning of your program,
AppDomain.CurrentDomain.UnhandledException += (a, exception) => File.AppendAllText("errorlog.txt", exception.ToString() + "\n"
This will keep the user from ever seeing ugly exceptions being thrown. Also be sure you are not giving the users the .pdb files as this will cause exceptions to contain paths of the computer it was compiled on which can contain your username and other sensitive information you wouldn't want a client to see.
You can register the global exception handling when the main window is initialized, you want it to be the first thing you do before any thing else because again you never know when an exception will be thrown so you have to think defensively.
public partial class MainWindow : Window
{
public MainWindow()
{
AppDomain.CurrentDomain.UnhandledException += (a, exception) => File.AppendAllText("errorlog.txt", exception.ToString() + "\n");
InitializeComponent();
}
}
C# uses exceptions extensively so it will be good concept for you to study up on if you are not familiar with this type of error handling. All exceptions derive from the Exception class so when you write catch (Exception e) this will catch all exceptions (because a base reference can hold an object of a derived type), however if you know the specific exception a method will throw you can catch a more specific exception (always before the more general catch) and handle it in a specific way. In this example you may have an IOException from the File.Move() that you want to catch and handle differently.
try
{
string pathUser4 = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload4 = (pathUser4 + #"\Downloads\");
string sourceFile = pathDownload4 + listBox1.Text;
string pathdoc5 = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string pathDownload5 = (pathdoc5 + #"\iracing\setups\");
string destinationFile = pathDownload5 + comboBox1.Text;
File.Move(sourceFile, destinationFile);
if (comboBox1.Text == "Select File Destination")
{
MessageBox.Show("Please Select A Destination Folder", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception e)
{
File.AppendAllText("ErrorLog.txt", e.ToString() + "\n");
}
The example code from MSDN for File.Move should get you pointed at the various things you need to deal with, such as an already existing file and basic error handling.
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = #"c:\temp\MyTest.txt";
string path2 = #"c:\temp2\MyTest.txt";
try
{
if (!File.Exists(path))
{
// This statement ensures that the file is created,
// but the handle is not kept.
using (FileStream fs = File.Create(path)) {}
}
// Ensure that the target does not exist.
if (File.Exists(path2))
File.Delete(path2);
// Move the file.
File.Move(path, path2);
Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now.
if (File.Exists(path))
{
Console.WriteLine("The original file still exists, which is unexpected.");
}
else
{
Console.WriteLine("The original file no longer exists, which is expected.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
The error may caused by your code, or by some invalid input.
As #Despertar mentioned, I suggest all the program include error handling and log features in your code. It will be very helpful for your debug.
But I suggest use open source log library, not do it by yourself. For example, log4net, NLog, etc.

Permission problem with bitmap.Save()

I have this simple code:
System.Drawing.Bitmap bm = bitmapSourceToBitmap(source);
try
{
bm.Save(#"C:\Seva\testeImagem.jpg");
}
catch (Exception ex)
{
}
This throws: Generic Error GDI+.
Anyway, I seached and people say that the problem is with permissions. How can I give permissions to it? Thanks
First find out under what credentials the code is running.
Then check (and, when needed, fix) the security/NTFS settings of the Seva folder.
Especially when this code is running from within a website or service the account will not have permissions to write to the folder.
instead of saving to C:\Seva\testeImagem.jpg why not try saving to
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"testeImagem.jpg");
You must ensure that the Seva folder exists under C:\ and ensure that the current user has permissions to write to\create this folder. Also, its considered bad practice to write to folders that the user doesn't own. If the user is Running As A Normal User (not an admin) failure to do so results in permission exceptions.
Could you test if the folder exists?
void BitmapCopy(System.Drawing.Bitmap source, string filename) {
if (!String.IsNullOrEmpty(filename) && (source != null)) {
string dirName = #"C:\Seva";
if (!System.IO.Directory.Exists(dirName)) {
dirName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
string bmpFile = System.IO.Path.Combine(dirName, filename);
System.Drawing.Bitmap bm = bitmapSourceToBitmap(source);
try {
bm.Save(bmpFile);
} catch (ArgumentNullException ex) {
Console.WriteLine(ex.Message);
} catch (System.Runtime.InteropServices.ExternalException ex) {
Console.WriteLine(ex.Message);
}
}
}

Categories

Resources