Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7792557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:13:18+00:00 2026-06-01T22:13:18+00:00

I have a WinForms application that I’d like to have ability to run as

  • 0

I have a WinForms application that I’d like to have ability to run as a console application too (with 2 command-line arguments).

I tried the approach from this question Behavior in WinForm/Console Hybrid Application. But in this case, the methods of the FormMain.cs (FormMain class) are not accessible from the Program class (Program.cs).

How should I modify the code to be able to execute the FormMain.cs methods from the Program.cs?

UPD

The Program.cs code is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Feature
{

internal static class NativeMethods
{
    [DllImport("kernel32.dll")]
    internal static extern Boolean AllocConsole();
}

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            // run as windows app
            Application.EnableVisualStyles();
            Application.Run(new FormMain(args));
        }
        else
        {
            // run as console app
            NativeMethods.AllocConsole();
            //??? How to access the methods of FormMain here ???
        }
    }
}

}

The FormMain.cs code (i have reduced it a bit) is

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 System.Collections;
using System.Xml.XPath;
using System.Xml;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace Feature
{
public partial class FormMain : Form
{
    public FormMain(string[] Args)
    {
        InitializeComponent();
    }

    public ArrayList Errors = new ArrayList();

    public ArrayList GetDirectoriesRecursively(string startPath)
    {
        string[] oNewDirectories = Directory.GetDirectories(startPath);
        ArrayList oDirectories = new ArrayList();
        ArrayList oDirectoriesNewList;

        foreach (string oCurrent in oNewDirectories)
        {
            oDirectories.Add(oCurrent);
            oDirectoriesNewList = GetDirectoriesRecursively(oCurrent);
            if (oDirectoriesNewList.Count > 0) oDirectories.AddRange(oDirectoriesNewList);
        }
        return oDirectories;
    }

    public ArrayList GetFilesInFolder(string startPath, string pattern)
    {
        string[] oNewFiles = Directory.GetFiles(startPath, pattern);
        ArrayList oFiles = new ArrayList();
        foreach (string oCurrent in oNewFiles)
            oFiles.Add(oCurrent);
        return oFiles;
    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:13:20+00:00Added an answer on June 1, 2026 at 10:13 pm

    You may be looking at this backward: Instead of hoping to get access to your useful functions found in Form.cs – if they’re that useful – you should, instead, move them to an external location where you can control their accessibility more easily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to write a console or winforms application that will interact with the
I have a WinForms Application that I want to run at Mono at some
I have a Winforms application that generates its own PrintDocument object for printing. It
We have a WinForms application that uses SQL server to store its data. To
I have a Winforms application that schedules some work using a service. The service
I have a Winforms application that dynamically instantiates external form objects for use in
I have a WinForms application that requires some configuration which I do at install
I have a WinForms application that makes use of a TaskDialog library that leverages
I have a WinForms application (WF), and a Library called by that WinForms application
I have a .net WinForms 2.0 application that has an image library of about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.