我有一个在测试和应用程序开发期间工作正常的应用程序,但是当我在外部系统中构建和安装应用程序时,它可以工作,然后在创建新窗口时抛出异常。这是表单的构造函数代码和Load事件
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();
}
这是抛出异常并关闭时的堆栈跟踪。
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()
在时间限制下,我们将非常感谢您对这些例外原因的任何帮助。
修改
这是句柄方法
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();
}
}
此方法由
调用 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();
}
}
答案 0 :(得分:1)
我对这两行代码感到好奇:
var erorPath = Path.Combine(documentsPath, "{0}_error.txt", fileName);
erorPath = Path.Combine(documentsPath, erorPath);
我认为这会导致您的错误,正如Matthew指出的那样,隐藏了另一个错误。
尝试将这两行改为此单词:
var erorPath = Path.Combine(documentsPath, string.Format("{0}_error.txt", filename));
答案 1 :(得分:0)
您正在尝试创建文档文件夹(如果该文件夹不存在)。以管理员身份运行您的应用。
答案 2 :(得分:0)
请检查您的文件夹/目录权限。 如果没有为所有用户提供所有必要的读取,写入等权限。