0

当我启动应用程序时,我得到异常可能是什么?

异常在构造函数中的一行:

checkBox1.Checked = Options_DB.Get_Automatic_Start();

这是options_db课程,但我在那里使用了一个断点,它永远不会到达options_db

这是在 form1 中引发异常的构造函数:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DannyGeneral;
using System.Drawing.Drawing2D;

namespace Batch_Images_Convertion
{
    public partial class Form1 : Form
    {
        int i;
        bool cancel_;
        bool automatic_;
        public Form1()
        {

            InitializeComponent();
            i = 0;
            automatic_ = false;
            checkBox1.Checked = Options_DB.Get_Automatic_Start();

它永远不会超出这条线。

这是异常错误消息:

System.TypeInitializationException was unhandled
  Message=The type initializer for 'Batch_Images_Convertion.Options_DB' threw an exception.
  Source=Batch_Images_Convertion
  TypeName=Batch_Images_Convertion.Options_DB
  StackTrace:
       at Batch_Images_Convertion.Options_DB.Get_Automatic_Start()
       at Batch_Images_Convertion.Form1..ctor() in D:\C-Sharp\Batch_Images_Convertion\Batch_Images_Convertion\Batch_Images_Convertion\Form1.cs:line 26
       at Batch_Images_Convertion.Program.Main() in D:\C-Sharp\Batch_Images_Convertion\Batch_Images_Convertion\Batch_Images_Convertion\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.DirectoryNotFoundException
       Message=Could not find a part of the path 'C:\Users\Chocolade\AppData\Local\Microsoft\Batch_Images_Convertion\settings\settings_convertion.txt'.
       Source=mscorlib
       StackTrace:
            at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
            at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
            at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
            at System.IO.File.Create(String path)
            at DannyGeneral.OptionsFile..ctor(String settings) in D:\C-Sharp\Batch_Images_Convertion\Batch_Images_Convertion\Batch_Images_Convertion\OptionsFile.cs:line 72
            at Batch_Images_Convertion.Options_DB..cctor() in D:\C-Sharp\Batch_Images_Convertion\Batch_Images_Convertion\Batch_Images_Convertion\Options_DB.cs:line 25
       InnerException: 
4

1 回答 1

1

看起来它试图加载文件'C:\Users\Chocolade\AppData\Local\Microsoft\Batch_Images_Convertion\settings\settings_convertion.txt'但目录settings不存在。

在您的构造函数中,通过执行以下操作检查以确保目录存在:

Directory.Exists(@"path\to\your\folder")

在尝试访问文件之前。

于 2012-08-19T22:21:09.843 回答