我目前正在学习.NET中的Symmetric Cryptography.我写了一个演示如下:
private byte[] key = Encoding.ASCII.GetBytes("abcdefgh");
private byte[] IV = Encoding.ASCII.GetBytes("hgfedcba");
private byte[] encrypted;
public Form1()
{
InitializeComponent();
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
this.textBox2.Text = this.Encrypt(this.textBox1.Text);
}
private void btnDecrypt_Click(object sender, EventArgs e)
{
this.textBox3.Text = this.Decrypt(this.textBox2.Text);
}
private string Encrypt(string plainText)
{
try
{
using (DESCryptoServiceProvider crypto = new DESCryptoServiceProvider())
{
crypto.Key = this.key;
crypto.IV = this.IV;
ICryptoTransform transform = crypto.CreateEncryptor(crypto.Key, crypto.IV);
using (MemoryStream stream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(stream, …
Run Code Online (Sandbox Code Playgroud) 我有一个C#enum类型,最终有很长的限定名.例如
DataSet1.ContactLogTypeValues.ReminderToFollowupOverdueInvoice.
Run Code Online (Sandbox Code Playgroud)
为了便于阅读,如果我能告诉某个特定的函数只使用名称的最后一部分,那会很好...
{
using DataSet1.ContactLogTypeValues;
...
logtype = ReminderToFollowupOverdueInvoice;
...
}
Run Code Online (Sandbox Code Playgroud)
是否有可能在C#中做这样的事情?
我使用表单身份验证cookie存储用户详细信息.
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, userName,DateTime.Now,DateTime.Now.AddMinutes(Timeout)false};
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
Run Code Online (Sandbox Code Playgroud)
如何获取添加的cookie和用户详细信息(authTicket)?
我有一系列数据结构,如:
abstract class Base {...}
class Foo : Base {...}
class Bar : Base {...}
Run Code Online (Sandbox Code Playgroud)
以及一个接受Base并根据它所依赖的子类转换它的方法:
void Convert(Base b) {
if (b is Foo)
// Do the Foo conversion
else if (b is Bar)
// Do the Bar conversion
...
Run Code Online (Sandbox Code Playgroud)
显然这是一个可怕的面向对象 - 转换方法必须知道Base的每个派生类,并且必须在每次扩展Base时进行更改.解决这个问题的'正常'OO方法是使每个派生类的Base负责转换自身,例如
abstract class Base {
abstract Converted Convert();
...}
class Foo : Base {
override Converted Convert(){...}
...}
class Bar : Base {
override Converted Convert(){...}
...}
Run Code Online (Sandbox Code Playgroud)
但是,在我编写的代码中,Base是一个纯数据结构(只有getter和setter - 没有逻辑),而且它在另一个我无权更改的程序集中.有没有一种方法可以更好地构造代码,而不强制Base的派生类具有逻辑?
谢谢
例外是代码:
private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
{
ActiveDownloadJob adJob = e.UserState as ActiveDownloadJob;
if (adJob != null && adJob.ProgressBar != null)
{
adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));
}
}
Run Code Online (Sandbox Code Playgroud)
在线上:
adJob.ProgressBar.Invoke((Action)(() => adJob.ProgressBar.Value = e.ProgressPercentage));
Run Code Online (Sandbox Code Playgroud)
这是form1中的ActiveDownloadJob类:
class ActiveDownloadJob
{
public DownloadImages.DownloadData DownloadData;
public ProgressBar ProgressBar;
public WebClient WebClient;
public ActiveDownloadJob(DownloadImages.DownloadData downloadData, ProgressBar progressBar, WebClient webClient)
{
try
{
this.DownloadData = downloadData;
this.ProgressBar = progressBar;
this.WebClient = webClient;
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定我是否需要调用此行,因为我现在不使用背景工作者,但我不确定.
这是完整的异常消息:在创建窗口句柄之前,无法在控件上调用Invoke或BeginInvoke
System.InvalidOperationException …
Run Code Online (Sandbox Code Playgroud) 我有一个表面上看起来很简单的问题,但我有很多问题需要解决。
我有2个双数组(左右声道),其中包含由DAQ以350KHz采样的数据,我已将其下变频为44.1KHz。
我要做的就是将这2个阵列播放并播放,但似乎有很多可供选择的声音输出选项,例如DirectX,NAudio等,所以我希望那里有人可以为我提供建议这样做的最佳方法是什么,也许可以指出正确的方向!
在此先感谢您的任何建议-我们将不胜感激。
戴夫
我试图在2周时间内完成C#的练习,为我的时间限制测试做准备,并且一直试图完成我在书籍和互联网上发现的练习.
该练习要求使用while循环来要求用户输入他们的名字,如果它不是"XXX",那么它可以继续循环.但是我得到的问题是,在编写循环之后,它只是继续,用户无法输入"XXX"来停止程序,所以我想知道是否有人知道这个解决方案?
这是我到目前为止写的代码..
String sName;
//Declaring the variable
Console.Write("Enter a name (or XXX to end): ");
sName = Console.ReadLine();
//Prompting user to enter the name or to end the program by telling them to type XXX
while (sName != "XXX")
{
Console.Write("The Name is: " + sName);
Console.WriteLine();
}
//Start loop
Console.WriteLine("You are now past the while loop");
//If XXX is typed, message is displayed
Console.WriteLine("Press any key to close");
Console.ReadKey();
//Prevent program from closing
Run Code Online (Sandbox Code Playgroud) 我想要一个显示单词Seq(这是一个列名)的文本框,然后列出其下面的mylist中的值.到目前为止,列表中的值显示,但Seq没有显示
private void button7_Click(object sender, EventArgs e)
{
if (seq1)
{
textBox1.Text = " Seq"; // This guy doesn't showup in the textbox
foreach (object o in SeqIrregularities)
{
textBox1.Text = String.Join(Environment.NewLine, SeqIrregularities);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我是C#的新手,只是比较两个数组...两个数组应该相等,因为它们的值相同,但是结果始终为假...不确定为什么。
char[] arOne = { 'a', 'b', 'c', '\u0000' };
char[] arTwo = new char[] { 'a', 'b', 'c', '\u0000' };
Console.WriteLine(" Two arrays are equal ? ...{0}", (arOne == arTwo) ? "true" : "false");
Run Code Online (Sandbox Code Playgroud) 我有一个具有ID列的数据库,ID列是BIGINT类型.我相信我的问题与转换ID列的正确类型有关,以使插入代码起作用.我试图转换它但无法获得正确的语法.
这是我的代码......
OleDbCommand cmd1 = new OleDbCommand("select ID, Name", con);
cmd1.Connection = con;
con.Open();
int result = (int)cmd1.ExecuteScalar();
sqlcmd.CommandText = "INSERT INTO MyTable(ID, Name) VALUES(@ID, @Name)";
sqlcmd.Parameters.Clear();
sqlcmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = result;
Run Code Online (Sandbox Code Playgroud)