0

I have some short code:

private void buttonSave_Click(object sender, EventArgs e) 
{

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Filter = "NHC|*.nhc";
    openFileDialog1.Title = @"test.nhc";

    OpenFileDialog openfiledialog = new OpenFileDialog();
    openfiledialog.ShowHelp = true;
    openfiledialog.FileName = "test.nhc";
    openfiledialog.ShowDialog();
}

I want to set the FileName in OpenFileDialog.
For example: I have a web app and I click Upload to upload a file from the local PC. The OpenFileDialog PopUp does open. Now I want to set the FileName to test.nhc in the field FileName (Windows window) and click "Open".

But it doesn't work.

zx485
  • 28,498
  • 28
  • 50
  • 59
Barpe2
  • 39
  • 1
  • 7
  • Let me understand your question. You want, when you press an UPLOAD button on an WEB application/page, choose what is the filename that appears in the system owned open file dialog? – Steve Mar 29 '17 at 12:48
  • I want to upload a file from local PC. On layout is button UPLOAD. Then I want to sort files to .nhc extension, set test.nhc in FILENAME windows window and Click OPEN. Next validate that new version .nhx is on web app (e.g. using by Webdruver). – Barpe2 Mar 29 '17 at 13:12
  • It is quite similar like in Zamzar.com, when You click [Choose], windows pop up dialog, then You put File Name and Click Open. I want to automate these steps in C#. – Barpe2 Mar 29 '17 at 13:44
  • It was quite simple sollution: Thread.Sleep(500); SendKeys.SendWait(RepoFiles.File_Name); Thread.Sleep(500); SendKeys.SendWait(RepoButtons.Enter); – Barpe2 Mar 30 '17 at 10:51

2 Answers2

0

Do you want to set the name before or after clicking? You should define this name in PageLoad, for example. Not at Click.

OpenFileDialog openfiledialog = new OpenFileDialog();

protected void Page_Load(object sender, EventArgs e)
{    
         if (!IsPostBack)
         {
            openfiledialog.FileName = "test.nhc";
         }
}

private void buttonSave_Click(object sender, EventArgs e) 
{
          openfiledialog.ShowDialog();
}
Guilherme Fidelis
  • 1,022
  • 11
  • 20
0

You are not handling the event for open file ok button. First you need to create a Stream object and then an event that will happen in DialogResult.OK situation.

Here is an example from microsoft

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "NHC|*.nhc";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Title = @"test.nhc";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFileDialog1.OpenFile()) != null)
                    {
                        using (myStream)
                        {
                            // Insert code to read the stream here.
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }



  
        }
    }