petko_stankoski
petko_stankoski

Reputation: 10713

C# result is fine when I am debugging, but I get errors when I start the .exe file

I work in C#, .Net 2.0.

When I debug my code everything works fine. However, when I create the exe, it isn't. I added a logger. Here is a piece of my code:

string[] dllFiles = System.IO.Directory.GetFiles(getAddinDllPath(), "Begin_*.dll");
foreach (string dllFile in dllFiles)
{
    Logger.Info("ThisMethod numberX");
    if (!dllFile.EndsWith("someString.dll", StringComparison.InvariantCultureIgnoreCase))
    {
        Assembly assembly = Assembly.LoadFile(dllFile);

        foreach (Type type in assembly.GetTypes())
        {
            Logger.Info("ThisMethod  numberY");
         ...............

The getAddinDllPath() method returns this:

addinDllPath = Path.Combine(Path.Combine(myDocuments, "Visual Studio 2005"), "Addins") + "\\";

When I debug my program, the text 'ThisMethod number Y' shows up multiple times in the log file.

When I create the exe and run it, the logger looks like this: ThisMethod numberX at the end. It never comes to "ThisMethod numberY".

Why is that?

Here is my logger:

class Logger { private static string path = @"C:\Users\John\Documents\Projects\MyLogger.txt";

    public static string Path
    {
        get { return path; }
        set { path = value; }
    }

    public static void Info(string msg)
    {
        StreamWriter sw = File.AppendText(path);
        try
        {
            string logLine = System.String.Format(
                "{0:G}: {1}: {2}", System.DateTime.Now, @"INFO", msg);
            sw.WriteLine(logLine);
        }
        finally
        {
            sw.Close();
        }
    }
}

Edit: if dllFiles is empty, then everything is fine. But if the files are there, then the problem appears.

Edit 2: The problem is in

Type type in assembly.GetTypes()

Has somebody an idea?

Upvotes: 0

Views: 134

Answers (4)

petko_stankoski
petko_stankoski

Reputation: 10713

I should have used

Assembly assembly = Assembly.LoadFrom(dllFile);

instead of

Assembly assembly = Assembly.LoadFile(dllFile);

Upvotes: 0

curiousity
curiousity

Reputation: 4741

Try to use IndexOf >=0 not EndsWith("someString.dll" sometimes it bugging 2. May be one of types defined or moved in other assembly / Look at msdn GetTypes where described exception

Upvotes: 0

Sergii Kudriavtsev
Sergii Kudriavtsev

Reputation: 10512

if "somePath" is relative to run directory then your exe might be scanning another directory => finding no .dll files => "ThisMethod numberY" is not called.

If my guess is true - try specifying absolute path in somePath.

Upvotes: 3

hungryMind
hungryMind

Reputation: 6999

Ensure string dllFile in dllFiles gives correct address of dll's & ends with string you mentioned in code and ensure log file is closed at the end. You will definitely see it.

Upvotes: 2

Related Questions