I have an application that is working fine during testing and application development, but when I build and install the application in an external system, it works, then throws an exception when creating a new window. Here is the form's constructor code and Load event
public VoucherForm(int transactionId)
{
InitializeComponent();
_voucherTransactionId = transactionId;
_settingService = new SettingService();
_transactionService = new TransactionService();
_voucherController = new PaymentVoucherController();
}
private void VoucherForm_Load(object sender, EventArgs e)
{
_panelWidth = voucherPanel.Width;
_paymentVoucher = PaymentVoucherController.GetPaymentVoucher;
if (_paymentVoucher == null)
{
var voucher = _voucherController
.ReloadVoucher(_voucherTransactionId);
if (voucher == null)
{
const string msg = "Cannot display voucher.";
MessageBox.Show(msg, Resources.ActionNotSuccessful,
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
_paymentVoucher = voucher;
}
LoadVoucherTitles();
//Load payment voucher
LoadPaymentVoucher();
}
and here is the stack trace when it throws the exception and shuts down.
The process was terminated due to an unhandled exception.
Exception Info: System.IO.DirectoryNotFoundException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.FileStream.Init(System.String, System.IO.FileMode, System.IO.FileAccess, Int32, Boolean, System.IO.FileShare, Int32, System.IO.FileOptions, SECURITY_ATTRIBUTES, System.String, Boolean, Boolean, Boolean)
at System.IO.FileStream..ctor(System.String, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, Int32)
at PPSMB.UI.Common.ExceptionHandler.Handle(System.Exception)
at PPSMB.UI.MainForm.Application_ThreadException(System.Object, System.Threading.ThreadExceptionEventArgs)
at System.Windows.Forms.Application+ThreadContext.OnThreadException(System.Exception)
at System.Windows.Forms.Control.WndProcException(System.Exception)
at System.Windows.Forms.Control+ControlNativeWindow.OnThreadException(System.Exception)
at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
at PPSMB.UI.Program.Main()
Please any help as to the cause of these exception will be greatly appreciated as am under the constraint of time.
EDIT
Here is the handle method
public static void Handle(Exception exception)
{
var documentsPath = Environment
.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var exceptionString = exception.ToString();
documentsPath = Path.Combine(documentsPath, "Ppsmb");
if (!Directory.Exists(documentsPath))
{
Directory.CreateDirectory(documentsPath);
}
var fileName = Path.GetRandomFileName();
var erorPath = Path.Combine(documentsPath, "{0}_error.txt", fileName);
erorPath = Path.Combine(documentsPath, erorPath);
FileStream stream = null;
if (!File.Exists(erorPath))
{
stream = File.Create(erorPath);
}
else
{
stream = File.OpenWrite(documentsPath);
}
using (var strStream = new StreamWriter(stream))
{
strStream.WriteLine(exceptionString);
strStream.Flush();
strStream.Close();
stream.Close();
}
}
This method is called by
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs args)
{
const string exceptionMsg = "An error has occured!, " +
"please close the application and " +
"restart it again.";
Exception exception = args.Exception;
ExceptionHandler.Handle(exception);
DialogResult dialogResult = MessageBox.Show(exceptionMsg, "ERROR!",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (dialogResult == DialogResult.OK)
{
Application.Exit();
}
}
I'm curious about these two lines of code:
var erorPath = Path.Combine(documentsPath, "{0}_error.txt", fileName);
erorPath = Path.Combine(documentsPath, erorPath);
I think this is causing your error, which as Matthew pointed out, is hiding another error.
Try changing those two lines to this single:
var erorPath = Path.Combine(documentsPath, string.Format("{0}_error.txt", filename));
You are attempting to create the documents folder if it does not exist. Run your app as Administrator.
please check your folder/directory permission. If not give all the necessary read,write etc rights for all the users.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.