0

I'm a newbie in c# programming and I hope this is an easy question.

I have this code that was working fine on Visual Studio 2010 with WinXP:

        SaveFileDialog saveFile2 = new SaveFileDialog();

        saveFile2.InitialDirectory = "C:\\BARCODE";

        saveFile2.FileName = "modulaListaV.txt";
        saveFile2.DefaultExt = "*.txt";
        //saveFile2.Filter = ".txt File|*.txt";

        SendKeys.Send("{ENTER}");

        if (saveFile2.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
             saveFile2.FileName.Length > 0)
        {

            richTextBox3.SaveFile(saveFile2.FileName, RichTextBoxStreamType.PlainText);
        }         

After I moved to Win 7 and Visual Studio 2015, it's not firing the ENTER key anymore.

I tried also to replace the SendKeys with this:

InputSimulator.SimulateKeyPress(VirtualKeyCode.RETURN); (had to add the specific .dll)

But that's not working either.

Anyone that can please help?

Thanks in advance.

Lorrido

  • Possible duplicate of [SendKeys.send is blocked](http://stackoverflow.com/questions/4665952/sendkeys-send-is-blocked) – fruggiero Jun 13 '16 at 13:47
  • I add the same issue, most of the time windows skip it (not the code) try to add a System.Threading.Thread.Sleep(5); before your SendKeys.Send("{ENTER}");. Window sometimes doesn't have "the time" to receive the keysend. – jsls Jun 13 '16 at 14:02
  • Thanks Jrsls but unfortunately that's not fixing it. – Lorrido Welcome Jun 13 '16 at 14:16
  • What are you trying to achieve? You are opening a SaveFileDialog, which is intended to interact with the user but then you are sending an Enter key message which won't let the user to give his input? – ehh Jun 13 '16 at 14:21
  • I just want to automatic save the .txt file, there's no need for user's input. – Lorrido Welcome Jun 13 '16 at 14:24
  • So you don't need a SaveFileDialog. Just pass the full file name as parameter to the SaveFile method of richTextBox3 – ehh Jun 13 '16 at 14:25

2 Answers2

1

No need for SaveFileDialog since you do not need user intervention/input.

Try the following instead:

var initialDirectory = @"C:\BARCODE";
var fileName = "modulaListaV.txt";
var fullFileName = Path.Combine (initialDirectory ,fileName );

richTextBox3.SaveFile(fullFileName,RichTextBoxStreamType.PlainText);

or shorter:

richTextBox3.SaveFile(@"C:\BARCODE\modulaListaV.txt",RichTextBoxStreamType.PlainText);
ehh
  • 3,412
  • 7
  • 43
  • 91
0
    SaveFileDialog saveFile2 = new SaveFileDialog();

    saveFile2.InitialDirectory = "C:\\BARCODE";

    saveFile2.FileName = "modulaListaV.txt";
    saveFile2.DefaultExt = "*.txt";
    //saveFile2.Filter = ".txt File|*.txt";

    System.Threading.Thread.Sleep(1000)

    SendKeys.Send("{ENTER}");

    if (saveFile2.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
         saveFile2.FileName.Length > 0)
    {

        richTextBox3.SaveFile(saveFile2.FileName,RichTextBoxStreamType.PlainText);
    }      
Massaoui
  • 58
  • 7