GiggyLapisar
GiggyLapisar

Reputation: 105

Initialisation Error regarding a Windows Form

I am programming a window form application and have managed to remove all errors from the forms coding. However when I attempt to boot the application I get the exception:

Exception thrown: 'System.TypeInitializationException' in Mod Note 2.0.exe

Additional information: The type initializer for 'Mod_Note_2._0.Form1' threw an exception.

The form's code reads as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace Mod_Note_2._0
{
    public partial class Form1 : Form
    {
    //Defines variables 
    
    public static string moduleTitle;
    public static string[] codRetr = Directory.GetFiles("" , "*code.txt");
    public static string[] notes;
    public static int i = 0;
    public static List<string> codDrop;
    public string[] codDropT;

    public Form1()
    {
        InitializeComponent();
    }

    private void programmingAndDataStructureToolStripMenuItem_Click(object sender, EventArgs e)
    {
        moduleTitle = "CMP1127M";
        SetTextBoxes item = new SetTextBoxes();
        item.moduleTitle1 = moduleTitle;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void ModuleSummary_TextChanged(object sender, EventArgs e)
    {
        SetTextBoxes.modSynopsis = ModuleSummary.Text;
        File.WriteAllText(SetTextBoxes.modSynLocation, SetTextBoxes.modSynopsis);
    }

    private void AssignBox_TextChanged(object sender, EventArgs e)
    {
        SetTextBoxes.modAssign = AssignBox.Text;
        File.WriteAllText(SetTextBoxes.modAssignLoc, SetTextBoxes.modAssign);
    }

    private void AddNewModule_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
    }

    //Manages deleting of modules
    private void deleteCurrentModuleToolStripMenuItem_Click(object sender, EventArgs e)
    {
        File.Delete(SetTextBoxes.modNoLocation);
        File.Delete(SetTextBoxes.modTitleLocation);
        File.Delete(SetTextBoxes.modSynLocation);
        File.Delete(SetTextBoxes.modLOsLocation);
        File.Delete(SetTextBoxes.modAssignLoc);
    }

    private void modulesToolStripMenuItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    }

    //Puts the information into text boxes
    private class SetTextBoxes
    {
        public string moduleTitle1;
        public static string modNoLocation;
        public static string modNo;
        public static string modTitle;
        public static string modTitleLocation;
        public static string modSynopsis;
        public static string modSynLocation;
        public static string modLOs;
        public static string modLOsLocation;
        public static string modAssign;
        public static string modAssignLoc;
        public static string notesLoc;

        public SetTextBoxes()
        {
            Form1 frm = new Form1();
            notesLoc = moduleTitle + "N";

            modNoLocation = moduleTitle1 + "code.txt";
            modNo = File.ReadAllText(modNoLocation);
            frm.ModuleCode.Text = modNo;

            modTitleLocation = moduleTitle + "title.txt";
            modTitle = File.ReadAllText(modTitleLocation);
            frm.Title.Text = modTitle;

            modSynLocation = moduleTitle + "synopsis.txt";
            modSynopsis = File.ReadAllText(modSynLocation);
            frm.ModuleSummary.Text = modSynopsis;


            modLOsLocation = moduleTitle + "LOs.txt";
            modLOs = File.ReadAllText(modLOsLocation);
            frm.LOsTextbox.Text = modLOs;

            modAssignLoc = moduleTitle + "assignments.txt";
            modAssign = File.ReadAllText(modAssignLoc);
            frm.AssignBox.Text = modAssign;

            //Resets the Notes array
            Array.Clear(notes, 0, 1000000);

            //Retrieves and sets the notes
            notes = Directory.GetFiles("", notesLoc);
            
        }


    }

    private void modulesToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }



    private void modulesToolStripMenuItem_DropDownOpened_1(object sender, EventArgs e)
    {
    }

    private void modulesToolStripMenuItem_DropDownOpened(object sender, EventArgs e)
    {
        Array.Clear(codRetr, 0, 1000000);
        i = 0;

        foreach (string s in codRetr)
        {
            codDrop.Add(s);
        }

        codDropT = codDrop.ToArray();
    }

        

    private void button1_Click(object sender, EventArgs e)
    {
        Form3 form3 = new Form3();
        form3.Show();
    }
}

}

Edit: Here's where the Exception occurs Right at initialisation. For some reason

The Exception Code

System.TypeInitializationException was unhandled 
HResult=-2146233036
Message=The type initializer for 'Mod_Note_2._0.Form1' threw an exception.
Source=Mod Note 2.0
TypeName=Mod_Note_2._0.Form1
StackTrace:
at Mod_Note_2._0.Form1..ctor()
at Mod_Note_2._0.Program.Main() in           
C:\Users\Samuel\Documents\Computer_Science\PaDS\Mod Note 2.0\Mod Note 
2.0\Program.cs:line 19
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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: 
   HResult=-2147024809
   Message=The path is not of a legal form.
   Source=mscorlib
   StackTrace:
        at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths)
        at System.IO.Path.GetFullPathInternal(String path)
        at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
        at System.IO.Directory.GetFiles(String path, String searchPattern)
        at Mod_Note_2._0.Form1..cctor() in C:\Users\Samuel\Documents\Computer_Science\PaDS\Mod Note 2.0\Mod Note 2.0\Form1.cs:line 19
   InnerException:

Upvotes: 0

Views: 221

Answers (1)

yousif.aljamri
yousif.aljamri

Reputation: 428

This is the line causes the exception:

public static string[] codRetr = Directory.GetFiles("" , "*code.txt");

GetFiles the first parameter should be a path

if you want the current directory you can put "."

so your line would be : public static string[] codRetr = Directory.GetFiles("." , "*code.txt");

Upvotes: 1

Related Questions