9

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...>
    }  
}
Chris
  • 934
  • 1
  • 17
  • 38
  • 1
    Why don't you simple create a OpenFileDialog directly in your console application? – Styxxy Sep 23 '12 at 16:18
  • @Styxxy It is not possible because we don't have access to the `System.Windows.Forms` – Miro Markaravanes Sep 23 '12 at 16:26
  • 1
    @Miro you need to reference it to gain access. But you can't simply transfer the file name from your Windows Forms application to your Console application: they run on different processes, each has its own `Main` method. – Adam Sep 23 '12 at 16:56
  • I was able to get the dialog box to show up by adding 'using System.Windows.Forms' and the OFD that I posted above + 'excel.FileName = docPath.ToString()' but now none of my console comments are showing? – Chris Sep 23 '12 at 17:39
  • figured that one: http://stackoverflow.com/questions/5706723/windows-form-console-writeline-where-is-console?rq=1 – Chris Sep 23 '12 at 17:42

2 Answers2

17
  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.
        }
    }
}
Adam
  • 15,537
  • 2
  • 42
  • 63
  • 4
    This is not legal, an STA thread *must* pump a message loop. Side effects of not doing so is deadlock and events that don't fire. Particularly a problem when using COM objects. The OP is trying to get a little pregnant. – Hans Passant Sep 23 '12 at 17:43
  • 2
    @HansPassant interesting. I wasn't aware of this and would be happy to upvote a better solution. – Adam Sep 23 '12 at 17:52
-1

If you use STAThread you can not use await in your main function. This can be solved by creating a new thread in STA mode which calls the FolderBrowserDialog or OpenFileDialog.

    static void Main(string[] args)
    {
        Thread T = new Thread(new ThreadStart(OpenFolderBrowser));
        T.SetApartmentState(ApartmentState.STA);
        T.Start();
    }

    private static void OpenFolderBrowser()
    {
        using (OpenFileDialog _FolderBrowserDialog = new OpenFileDialog())
        {
            if (_FolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine(_FolderBrowserDialog.FileName);
            }
        }
    }
Naresh Bisht
  • 645
  • 6
  • 16