如何使用打开文件对话框?

use*_*173 0 c# filedialog winforms

我正在尝试使用公钥编写代码来加密文本,并使用私钥和密码来解密.

我对编程语言不太熟悉,因为我不是编程学生.但对于我的迷你项目,我需要编写一些关于加密的程序.

对于以下代码,使用我的c驱动器中的文本文件来使用公钥进行编码.但我想使用openfiledialog来选择文件,而不是手动指向它(不是很实用)

真的很感激,如果有人可以帮我编辑代码.PS我真的不知道如何将openfiledialog应用于我的代码.当我使用youtubes和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)
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Adr*_*tti 5

假设你正在使用WinForms.

只需创建一个OpenFileDialog,调用的实例,ShowDialog如果用户没有取消操作,则读取FileName属性:它将包含所选文件的完整路径.在代码中:

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

new FileInfo(dlg.FileName, passphraseTextBox.Text);
Run Code Online (Sandbox Code Playgroud)

当然您可能需要让用户快速过滤要显示的文件,您可以使用以下Filter属性:

var dlg = new OpenFileDialog();
dlg.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
Run Code Online (Sandbox Code Playgroud)

您甚至可以允许多项选择,设置Multiselecttrue,您将获得FileNames属性中的所有选定文件:

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);
    // ...
}
Run Code Online (Sandbox Code Playgroud)