how to use open file dialog?

半腔热情 提交于 2020-12-06 13:31:52

问题


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)
        {

        }
    }
}

回答1:


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);
    // ...
}



回答2:


private void decryptButton_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
    try
            {
                String plainString = pgp.DecryptString(encryptedtextTextBox.Text,new FileInfo(openFileDialog1.FileName), passphraseTextBox.Text);
                decryptedtextTextBox.Text = plainString;
                encryptedtextTextBox.Text = "";
                passphraseTextBox.Text = "";
            }
            catch
            {
                MessageBox.Show("ERROR! Please check passphrase and do not attempt to edit cipher text");
            }
     }
}


来源:https://stackoverflow.com/questions/19660775/how-to-use-open-file-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!