[英]How to append text to the richTextBox from another class in C# Winform?
[英]How to use a string from another class in c# winform application?
所以我在 Visual Studio C# 中編寫了一個 winform 應用程序,它使用生成的密鑰鎖定用戶類型的消息。 用戶需要知道密鑰的創建日期,該日期是從用戶鍵入“textBox1”時程序生成的文件中收集的。 我正在做的是使用字符串來驗證“password.txt”文件的創建日期,如果不是用戶輸入的日期,則消息將不會打開。 問題雖然我在使用 static 屬性時遇到問題,但此錯誤:
錯誤 CS0120 非靜態字段、方法或屬性“Encryptor1.Encrypt(string)”UniveralWindowsTextPGP C:\Users\keife\source\repos\UniveralWindowsTextPGP\Univeral\UniveralWindowsTextPGP\UniveralWindowsTextPGP 需要 object 引用
這是我實現加密和解密方法的地方:
namespace UniveralWindowsTextPGP
{
class Encryptor1
{
public static string IV = "1a1a1a1a1a1a1a1a";
public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13";
public static string Encrypt(string decrypted)
{
byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return Convert.ToBase64String(enc);
}
public static string Decrypted(string encrypted)
{
DateTime creation = File.GetCreationTime(@"C:\password.txt");
FileInfo fi = new FileInfo(@"C:\password.txt");
var created = fi.CreationTime;
if (fi.CreationTime != Form2.keyhere)
// 上面一行是這部分發生錯誤的地方。
{
string message = "That is incorrect, access is denied.";
MessageBox.Show(message);
}
else
{
byte[] textbytes = Convert.FromBase64String(encrypted);
AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
endec.BlockSize = 128;
endec.KeySize = 256;
endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
endec.Padding = PaddingMode.PKCS7;
endec.Mode = CipherMode.CBC;
ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
icrypt.Dispose();
return System.Text.ASCIIEncoding.ASCII.GetString(enc);
}
}
} }
// 這里是表格:
namespace UniveralWindowsTextPGP
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void passwordbox_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged_1(object sender, EventArgs e)
{
}
private void Decrypt_Click(object sender, EventArgs e)
{
string dir = richTextBox1.Text;
StreamReader sr = new StreamReader("encryptedmessagehere.txt");
string line = sr.ReadLine();
richTextBox1.Text = Encryptor1.Decrypted(Convert.ToString(line));
}
private void Form2_Load_1(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
string dir = richTextBox2.Text;
string enctxt = Encryptor1.Encrypt(richTextBox2.Text);
System.IO.File.WriteAllText(@"C:\encryptedmessagehere.txt", enctxt);
}
private void label4_Click(object sender, EventArgs e)
{
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
richTextBox2.EnableContextMenu();
}
private void button2_Click(object sender, EventArgs e)
{
string dir = richTextBox2.Text;
StreamReader sr = new StreamReader(@"C:\encryptedmessagehere.txt");
string line = sr.ReadLine();
richTextBox2.Text = Encryptor1.Decrypted(Convert.ToString(line));
System.IO.File.WriteAllText(@"C:\decryptedmessagehere.txt", line);
}
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Form f4 = new Form4();
f4.Show();
}
public void passwordbox1_TextChanged(object sender, EventArgs e)
{
}
private void richTextBox3_TextChanged(object sender, EventArgs e)
{
string richTextBox3 = "";
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("C:/password.txt");
txt.Write(textBox1.Text);
txt.Close();
public string keyhere = textBox1.Text;
// 這是這里發生錯誤的地方。
非常感謝,感謝您的想法。
這一行在這里:
public string keyhere = textBox1.Text;
在textBox1_TextChanged
方法中。
您在此行中使用訪問 static 變量的語法:
if (fi.CreationTime.= Form2.keyhere)
沒有 static 變量Form2.keyhere
。 這就是編譯器抱怨的原因。
您可以通過創建一個來解決這兩個問題:
public static string keyhere
public void textBox1_TextChanged(object sender, EventArgs e)
{
TextWriter txt = new StreamWriter("C:/password.txt");
txt.Write(textBox1.Text);
keyhere = textBox1.Text;
txt.Close(); // write this as last call, otherwise textBox1 might already be disposed when you try to access textBox1.Text;
}
免責聲明:這是一個臨時解決方案,它違反了干凈編碼的某些規則。 特別是單一責任原則。 最好將所有必要的信息傳遞給Decrypted
方法並在那里使用它。 class Encryptor1
不應該知道有關Form
或其他 UI 元素的任何信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.