C# 为用户提供从文件系统中选择“.txt”的选项
我正在制作一个密码解密/加密机控制台应用程序 我想做的是为用户提供一个选项,使其能够从文件系统中选择一个.txt文件或在控制台窗口中键入。我知道如何读/写/保存“.txt”等,但我有点不确定实现它的最佳方法C# 为用户提供从文件系统中选择“.txt”的选项,c#,file,C#,File,我正在制作一个密码解密/加密机控制台应用程序 我想做的是为用户提供一个选项,使其能够从文件系统中选择一个.txt文件或在控制台窗口中键入。我知道如何读/写/保存“.txt”等,但我有点不确定实现它的最佳方法 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace Application { class Caesar {
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();
}
}
}
您希望他们能够选择从文件系统中选择文件还是自己键入加密/纯文本字符串 或者您希望他们能够从文件系统中选择一个文件,或者自己键入文件路径 假设你的意思是后者,这是我解决你问题的办法
[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".
}
}
一种可能的方法是允许用户通过将文件拖放到.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,则可以使用以下命令设置项目的命令行参数: