You must Sign In to post a response.
  • Category: Windows 8

    OpenFile Dialog Control in C# execution problem

    I opened the new project in that double clicked on OpenFileDialog control. Then I taken one textbox made it as multiline below that one button under that button I written the code which I mentioned below
    int size = -1;
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
    string file = openFileDialog1.FileName;
    try
    {
    string text = File.ReadAllText(file);
    size = text.Length;
    }
    catch (IOException)
    {
    }
    }
    Console.WriteLine(size);
    Console.WriteLine(result);
    Here after execution whenever I am going for open button its showing some exception error like below
    "Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process".
    I am a beginner so since from two days I am trying to sort it out but I am unable to do it, so please help me.
  • #763013

    Hello Navaneet,

    Refer the below code :

    using System.IO;

    private void btnSelectFile_Click(object sender, EventArgs e)
    {
    txtFilePath.Clear();
    txtContent.Clear();
    lblLength.Text = "Length of file :";

    OpenFileDialog dialog = new OpenFileDialog();
    dialog.Filter = "All files (*.*)|*.*";
    dialog.FilterIndex = 1;
    dialog.InitialDirectory = "C:\\";
    dialog.Title = "Select file";
    if (dialog.ShowDialog() == DialogResult.OK)
    {
    txtFilePath.Text = dialog.FileName;
    txtContent.Text = File.ReadAllText(txtFilePath.Text.ToString());
    lblLength.Text = "Length of file : " + txtContent.TextLength;
    }
    }

    See the attached image. It's an output i have got using above code.

    Hope this is what you are looking for.


    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #763015
    Hi

    I modified your code, now it wil work as you expected.
    Write your code in button click event. After you select file and click ok, then output is shown in the multiline textbox.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Windows.Forms;

    namespace DisplaySelecedfileLenghth
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    int size = -1;
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
    string file = openFileDialog1.FileName;
    try
    {
    string text = File.ReadAllText(file);
    size = text.Length;
    }
    catch (IOException)
    {
    textBox1.Text = "invalid file selected";
    }
    }

    textBox1.Text = "the size is : " + size + " and result is : " + result;
    }
    }
    }

    Find the attachment of output.

    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment

  • #763028
    Hi,

    The error message clearly suggest you to add STA thread attribute before OLA Call's.


    i.e. Add STAThread attribute before your click function, like below.

    [STAThread]
    protected void btnclick(object sender, EventArgs e)
    {
    //do your stuff
    }


    Hope this will helpful to you...

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments