Daniel Lip
Daniel Lip

Reputation: 11319

I'm getting type initializer exception and can't find out what's wrong

When I start the application I'm getting the exception what could it be ?

The exception is on the line in the constructor:

checkBox1.Checked = Options_DB.Get_Automatic_Start();

This is the options_db class but I used a breakpoint there and it never get to the options_db

This is in form1 the constructor where the exception fire up:

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();

It never continues beyond this line.

This is the exception error message:

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: 

Upvotes: 0

Views: 1273

Answers (1)

poy
poy

Reputation: 10507

Looks like its trying to load the file 'C:\Users\Chocolade\AppData\Local\Microsoft\Batch_Images_Convertion\settings\settings_convertion.txt' but the directory settings doesn't exist.

In your constructor, check to make sure the directory exist by doing:

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

Before trying to access the file.

Upvotes: 1

Related Questions