views:

214

answers:

3

I need a sample/example demonstrating how to pass a config file as a parameter to a console application in .Net

+1  A: 

Pass it in command line parameters, in args[]. Something like this.

class Program
{
    static void Main(string[] args)
    {
        if (args == null)
        {
            Console.WriteLine("args is null."); // Check for null array
        }
        else
        {
            // use args to get passed config file path
        }
    }
}

~~~ How to call the program ~~~

C:\ConsoleApplication1.exe "your config file path" (like C:\config\app.config)

Incognito
A: 

Do you have access to target console application source code? Is it .NET application?

If yes do next: add target app as a reference to the source application project (exe can be added like dll, there is no difference). And call some public method.

// target.exe code
namespace Target {
   public class MyConfig { }

   public class Program {
      static void Main(string[] args) { }
      public static void EntryPoint(MyConfig conf) { }
   }
}

// source.exe code
namespace Source {
   class Program {
      static void Main(string[] args) {
         Target.MyConfig conf = new Target.Config();
         Target.Program.EntryPoint(conf);
      }
   }
}
abatishchev
A: 

If u want to store data like FileOutputDirectory you can also use Settings page instead of config file. Settings page is easy to configure and use. Read more at msdn website: link text

zgorawski