how to use open file dialog?

user2930173 picture user2930173 · Oct 29, 2013 · Viewed 13.2k times · Source

i am trying to write a code to encrypt text using public key and decrypt using private key and passphrase.

I am not very good with programming language because i'm not a programming student. But for my mini-project, i need to write some program about encryption.

For the below code uses a text file from my c drive to encode using the public key. But i want to use openfiledialog to choose the file instead of directing it manually(not really practical)

Really appreciate if anyone can help me edit the codes. P.S. i don't really know how to apply openfiledialog to my code. i keep getting errors when i use informations from youtubes and google.

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.Windows.Forms;
using System.IO;
using DidiSoft.Pgp;

namespace TEST2
{
    public partial class Form1 : Form
    {
        PGPLib pgp = new PGPLib();
        public Form1()
        {
            InitializeComponent();
        }

        private void encryptButton_Click(object sender, EventArgs e)
        {
            string testingstring = pgp.EncryptString(testTextBox.Text, new FileInfo(@"c:\TCkeyPublic.txt"));
            encryptedtextTextBox.Text = testingstring;
        }

        private void decryptButton_Click(object sender, EventArgs e)
        {
            try
            {
                String plainString = pgp.DecryptString(encryptedtextTextBox.Text,
                new FileInfo(@"c:\TCkeyPrivate.txt"), passphraseTextBox.Text);
                decryptedtextTextBox.Text = plainString;
                encryptedtextTextBox.Text = "";
                passphraseTextBox.Text = "";
            }
            catch
            {
                MessageBox.Show("ERROR! Please check passphrase and do not attempt to edit cipher text");
            }
        }

        private void passphraseTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Answer

Adriano Repetti picture Adriano Repetti · Oct 29, 2013

Assuming you're using WinForms.

Just create an instance of OpenFileDialog, call ShowDialog and if user didn't cancel the operation then read FileName property: it'll contain the full path of selected file. In code:

var dlg = new OpenFileDialog();
if (dlg.ShowDialog() != DialogResult.OK)
    return;

new FileInfo(dlg.FileName, passphraseTextBox.Text);

Of course you may need to let user quickly filter files to display, you can use Filter property for that:

var dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";

You can even allow multiple selection, set Multiselect to true and you'll get all selected files in the FileNames property:

var dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
dlg.Multiselect = true;

if (dlg.ShowDialog() != DialogResult.OK)
    return;

foreach (var path in dlg.FileNames)
{
    new FileInfo(path, passphraseTextBox.Text);
    // ...
}