为用户提供选择' .txt'来自filesystem

时间:2015-11-06 15:26:52

标签: c# file

我正在制作一个密码解密/加密控制台应用程序。

我想要做的是为用户提供选项,以便能够从文件系统中选择.txt文件或键入控制台窗口。我知道如何阅读/写/保存' .txt'等等,但我对实施它的最佳方式有点不确定。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Application
{


class Caesar
{
    static void Main(string[] args)
    {

       try{
        Console.WriteLine("Enter Key:");
        int k = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("What would you like to do?");
        Console.WriteLine("");
        Console.WriteLine("[E]   Encrypt");
        Console.WriteLine("[D]   Decrypt");
        string choice =Convert.ToString(Console.ReadLine()).ToUpper();

            // WOULD LIKE TO GIVE THE USER THE CHOICE BETWEEN TYPING OR  ADDING A .TXT FILE FROM THE FILSYSTEM
        switch (choice)
        {
            case "E": Console.Write("Enter Plain Text:");
                      string pt = Console.ReadLine();
                      caesar_cipher(k, pt);
                       break;

            case "D": Console.Write("Type CipherText :");
                       string ct = Console.ReadLine();
                      caesar_decipher(k, ct);
                      break;

            default: Console.WriteLine("You've entered an incorrect option!");
                       break;
        }
       }
       catch(Exception)
       {
           Console.WriteLine ("The value you entered is incorrect");
           Console.WriteLine ("Press any key to try again");
              Console.ReadKey();
        }


    }
    static void caesar_cipher(int key, string pt)
    {
        int size = pt.Length;
        char[] value = new char[size];
        char[] cipher = new char[size];
        for (int r = 0; r < size; r++)
        {
            value[r] = Convert.ToChar(pt.Substring(r, 1));
        }

        for (int re = 0; re < size; re++)
        {
            int count = 0;
            int a = Convert.ToInt32(value[re]);
            for (int y = 1; y <= key; y++)
            {
                if (count == 0)
                {
                    if (a == 90)
                    { a = 64; }
                    else if (a == 122)
                    { a = 96; }
                    cipher[re] = Convert.ToChar(a + y);
                    count++;
                }
                else
                {
                    int b = Convert.ToInt32(cipher[re]);
                    if (b == 90)
                    { b = 64; }
                    else if (b == 122)
                    { b = 96; }
                    cipher[re] = Convert.ToChar(b + 1);

                }
            }
        }
        string ciphertext = "";

        for (int p = 0; p < size; p++)
        {
            ciphertext = ciphertext + cipher[p].ToString();
        }
        Console.WriteLine("Cipher Text=");
        Console.WriteLine(ciphertext.ToUpper());
    }


    static void caesar_decipher(int key, string ct)
    {
        int size = ct.Length;
        char[] value = new char[size];
        char[] cipher = new char[size];
        for (int r = 0; r < size; r++)
        {
            cipher[r] = Convert.ToChar(ct.Substring(r, 1));
        }

        for (int re = 0; re < size; re++)
        {
            int count = 0;
            int a = Convert.ToInt32(cipher[re]);
            for (int y = 1; y <= key; y++)
            {
                if (count == 0)
                {
                    if (a == 65)
                    { a = 91; }
                    else if (a == 97)
                    { a = 123; }
                    value[re] = Convert.ToChar(a - y);
                    count++;
                }
                else
                {
                    int b = Convert.ToInt32(value[re]);
                    if (b == 65)
                    { b = 91; }
                    else if (b == 97)
                    { b = 123; }
                    value[re] = Convert.ToChar(b - 1);

                }
            }
        }
        string plaintext = "";

        for (int p = 0; p < size; p++)
        {
            plaintext = plaintext + value[p].ToString();
        }
        Console.WriteLine("Plain Text=");
        Console.WriteLine(plaintext.ToLower());
        Console.ReadKey();
    }
    }
}

2 个答案:

答案 0 :(得分:0)

您是否希望他们能够在从文件系统中选择文件或自己键入加密/纯文本字符串之间进行选择?

或者您希望他们能够从文件系统中选择文件或者自己输入文件路径吗?

假设你的意思是后者,这是我解决问题的方法。

    [STAThread]
    static void Main(string[] args) {
        try {
            Console.Write("Enter key : ");
            int key = Convert.ToInt32(Console.ReadLine());
            Console.Write("Would you like to [E]ncrypt or [D]ecrypt? : ");
            string choice = Console.ReadLine().ToUpper();

            // Making the decision of writing the filepath or selecting the filepath.
            Console.Write("Would you like to [W]rite or [S]elect the file path? : ");
            string fileChoice = Console.ReadLine().ToUpper();

            string filePath = "";
            if(fileChoice == "W") {
                Console.WriteLine("Type the filepath the .txt file with your Cipher/Plain text");
                filePath = Console.ReadLine();
            }
            else {
                OpenFileDialog dial = new OpenFileDialog();
                dial.Filter = "Text files (*.txt)|*.txt";
                if (dial.ShowDialog() == DialogResult.OK)
                    filePath = dial.FileName;
            }

            string stringData;
            using (var reader = new StreamReader(filePath))
                stringData = reader.ReadToEnd();


            switch (choice) {
                case "E":
                    caesar_cipher(key, stringData);
                    break;

                case "D":
                    caesar_decipher(key, stringData);
                    break;

                default:
                    Console.WriteLine("You've entered an incorrect option!");
                    break;
            }

            Console.ReadLine();  // Added this to make sure you can read the end result.
        }
        catch (Exception) {
            Console.WriteLine("The value you entered is incorrect");
            Console.WriteLine("Press any key to try again");
            Console.ReadKey();
            Main(null); // This will call the main method allowing you to "try again".
        }
    }

答案 1 :(得分:0)

这样做的一种可能方法是允许用户通过将文件拖放到.exe上来启动程序 - 然后程序可以加密或解密文件内容。如果仅通过双击.exe启动程序,则可以假定它将从控制台加密或解密用户输入。

你可以这样做:

    public static void Main(string[] args)
    {
        // If args is not empty, then the user has dragged and dropped a file.
        bool inputFromFile = args.Length > 1;
        string input = string.Empty;
        if (inputFromFile)
        {
            // This contains the file path of the dragged and dropped file.
            string fileName = args[0];
            input = File.ReadAllText(fileName);
        }
        else
            input = Console.ReadLine();
    }

要对此进行测试,如果您使用的是Visual Studio,则可以使用以下命令设置项目的命令行参数:

Modification of command line arguments in Visual Studio project properties