【问题标题】:how to use open file dialog?如何使用打开文件对话框?
【发布时间】:2013-10-29 14:16:51
【问题描述】:

我正在尝试编写代码以使用公钥加密文本并使用私钥和密码进行解密。

我对编程语言不是很好,因为我不是编程学生。但是对于我的小项目,我需要编写一些关于加密的程序。

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

如果有人可以帮助我编辑代码,我将不胜感激。 附:我真的不知道如何将 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)
        {

        }
    }
}

【问题讨论】:

  • 创建一个OpenFileDialog 实例,并调用它的ShowDialog 方法。你没有尝试。
  • 我和大卫在一起,这对谷歌来说是一个非常容易的话题。

标签: c# winforms filedialog


【解决方案1】:

假设您使用的是 WinForms。

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

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

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

当然你可能需要让用户快速过滤文件来显示,你可以使用Filter属性来实现:

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

您甚至可以允许多选,将Multiselect 设置为true,您将在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);
    // ...
}

【讨论】:

    【解决方案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");
                }
         }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 2018-10-14
      • 2011-09-21
      • 2016-08-02
      相关资源
      最近更新 更多