Hemant Sharma
Hemant Sharma

Reputation: 13

C# Runtime Exception System.TypeInitializationException on some machines

I was trying a very basic script to pull the sizes of all the fires in a directories. Being new to the language, I am aware of the logic but not about the .NET framework. I am currently using .NET Framework 4.5.1 for building this.

Problem:

The tools runs fine meeting all the expectations on my machine and other machines in the lab but I am getting below error message when I am trying to run the script on other machine:

RunTime Exception on some machines

CODE:

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.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Diagnostics;
using System.Threading;
using System.Security.Cryptography;
using System.Numerics;
using System.Text.RegularExpressions;

namespace FolderSize
{
    class Program
    {


        public int foldersize = 0;
        public static float foldersizeInMB = float.Parse("0.0");
        public static BigInteger SizeInBytes = new BigInteger();
        static void Main(string[] args)
        {
            string directoryName = "";
            SizeInBytes = BigInteger.Parse("0");
            if (args.GetLength(0)<1)
            {
                Console.ResetColor();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("No path provided. Please run the command again with correct arguments");
                Console.WriteLine("Syntax: ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.Write("FolderSize ");
                Console.Write("Path_of_the_folder(InQuotes)");
                Console.ForegroundColor = ConsoleColor.White;


            }
            else
            {
                directoryName = args[0];
                if ( !directoryName.EndsWith(@"\"))
                {
                    directoryName += @"\";
                }



                DirectoryInfo dir = new DirectoryInfo(directoryName);

                if (dir.Exists)
                {
                    getdirNames(dir);
                    Console.Write((SizeInBytes / (1024 * 1024)).ToString());
                }
                else
                {
                    Console.Write("Directory Not Found");
                }

            }

            }


        private static void getdirNames(DirectoryInfo dir)
        {


            try
            {
                foreach (DirectoryInfo d in dir.GetDirectories())
                {

                    foldersizeInMB = float.Parse("0.0");


                    try
                    {
                        foreach (FileInfo f in d.GetFiles())
                        {

                            SizeInBytes += f.Length;
                            string text = f.FullName;

                            int level = text.Split('\\').Length - 1;
                            foldersizeInMB += ((float)f.Length/1024);
                            Console.WriteLine("File?" + level.ToString()  + "?" + f.FullName + "?" + ((float)f.Length/1024));
                        }
                        Console.WriteLine("Folder?" + (d.FullName.Split('\\').Length-1) + "?" + d.FullName + "?" + foldersizeInMB);
                        getdirNames(d);
                    }
                    catch (Exception)
                    {

                    }

                }


            }
            catch
            {
            }


        }

    }
}

Upvotes: 1

Views: 119

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156998

The problem is in the parsing of floats I guess. The other system you have probably has another decimal separator, most likely because it it set to another language.

This is the failing line:

public static float foldersizeInMB = float.Parse("0.0");

Which should be:

public static float foldersizeInMB = 0.0f;

Or if you need to parse it for some reason, use a culture that supports your format:

public static float foldersizeInMB = float.Parse("0.0", CultureInfo.InvariantCulture);

Upvotes: 2

Related Questions