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 8571003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:41:14+00:00 2026-06-11T18:41:14+00:00

I have created a console application and have it working the way I want

  • 0

I have created a console application and have it working the way I want it to. Using the “Add Item” > “Add Windows Form” option in VS2010, it has automatically created what I need. I have added a button and code to retrieve an Excel file (below) My question is:

How do I take the file they have created and use it in my program.cs “Main” area?

The code for the OpenFileDialog button click event, from the Form1.cs:

private void btnSelect_Click(object sender, EventArgs e)
{
OFD.openFileDialog OFD = new OpenFileDialog();
OFD.Multiselect = false;
OFD.Title = "Open Excel Document";
OFD.Filter = "Excel Document|*.xlsx;*.xls";
OFD.ShowDialog();
string docPath = OFD.FileName;
}

That part of my static main event I wish to make “docPath” from the program.cs file

static void Main(string[] args)
    {
        var excel = new ExcelQueryFactory();
        excel.FileName = @"C:\Users\Christopher\Desktop\BookData\TestResults.xls";
       <...code executed on opened excel file...>
     }

Thank you for your time.

This is my completed solution:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {

        var excel = new ExcelQueryFactory();
        OpenFileDialog OFD = new OpenFileDialog();
        OFD.Multiselect = false;
        OFD.Title = "Open Excel Document";
        OFD.Filter = "Excel Document|*.xlsx;*.xls";
        OFD.ShowDialog();
        string filePath = OFD.FileName;
        excel.FileName= filePath.ToString();
        <.the rest of my program is below...>
    }  
}
  • 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-11T18:41:15+00:00Added an answer on June 11, 2026 at 6:41 pm
    1. Right click your Console application, add reference, System.Windows.Forms.
    2. Add using System.Windows.Forms; to the beginning of your file.
    3. Add the [STAThread] attribute to your Main to make it compatible with the open file dialog.

    [STAThread]
    public static void Main(string[] args)
    {
        var dialog = new OpenFileDialog
                         {
                             Multiselect = false,
                             Title = "Open Excel Document",
                             Filter = "Excel Document|*.xlsx;*.xls"
                         };
        using (dialog)
        {
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                var excel = new ExcelQueryFactory { FileName = dialog.FileName };
                // code executed on opened excel file goes here.
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a small chatting application in C#, and started as a Console
I have created an android application that calls (using kSOAP library) a SOAP based
I have created one WCF service , which is working fine, now i want
I am working with a vb.net Windows Forms application which has Application Settings, running
I have a long-running console-based application Sender that sends simple text to STDOUT using
i have created a very simple console application in vb.net that emails me some
I have a console application from which I create a window. I can render
I have written a little console application that uses s#arp. I can create an
I have a new Codeblocks install with mingw32 compiler. Created new c++ console project
I have created a JSP / servlets application running in Tomcat 7. It runs

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.