|
Consider the following example:
using System;
using System.Drawing;
using System.Windows.Forms;
public class Test : Form
{
static void Main()
{
Test t = new Test();
t.ShowDialog();
Application.Run();
}
TextBox tb;
public Test()
{
tb = new TextBox();
tb.Location = new Point(8, 8);
Controls.Add(tb);
Button b = new Button();
b.Location = new Point(tb.Right + 8, tb.Top);
b.Text = "Click";
b.Click += new EventHandler(b_Click);
Controls.Add(b);
}
private void b_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
Application.Exit();
}
} Now, I'm not sure why you'd want to do this, but you could. It's most likely that this would be handy when you want to have a dialog that doesn't act as the main window handle for an application that receives general application messages posted to the pump. You'll notice (if you compile and run this) that closing the dialog doesn't quit the application. Perhaps someone might have a good reason for using such an approach.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Interesting. I never would have thought that the button click message would have been dispatched correctly in this code. Thanks for the example. The reason I was asking is because I have to be able to use DDE in a service or console application so I was experimenting with different ways of creating the message pump without displaying a form. I have successfully used Application.Run with the form parameter, but it seems like a hack since the form is always hidden. I wonder if I could do something similar with the NativeWindow class? (Just thinking out loud!)
By the way, DDE sucks.
|
|
|
|
|
Nice code. Exactly what i needed for a game .
|
|
|
|
|
Hi,in C# i can list all the users in the domain using active directory services.But i just want to list only active users,i.e.users that have been already login.thanks anyway.
|
|
|
|
|
You could query the Last-Logoff and Last-Logon AD attributes (see DirectoryEntry.Properties ) and see if the latter is later than the former (i.e., they logged on after they logged off).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi everyone,
In C# I can manipulate known objects in the registry using the Registry and RegistryKey classes. But I would like to know what is the equivalent of the RegQueryValueEx Win32 API in C#? Can anyone tell me please? I would like to know (and therefore handle) the type (REG_SZ, REG_MULTI_SZ...) of a given value in the registry when I browse subkeys and hives.
Thanks for the answers.
|
|
|
|
|
i got two buttons in the form. One is for encrypting the message read from a xml file and then encrypt the xml and save back to another file. The other button is for decryption. Again, this button read the encrypted file back, and then perform decryption.
But i don't know why an exception always thrown when processing this line of code "int actualbytesread = encStream.Read(buf, 0, 100);" ... Can anyone give me some instructions what is happening ?
private void Encrypt_Click(object sender, System.EventArgs e)
{
XmlDocument xml = new XmlDocument();
xml.Load(Application.StartupPath + "\\Data\\userInfo.xml");
Byte[] data = new UnicodeEncoding().GetBytes(xml.OuterXml);
MemoryStream ms = new MemoryStream(data.Length* 2);
DES des = new DESCryptoServiceProvider() ;
CryptoStream encStream = new CryptoStream(ms,
des.CreateEncryptor(),
CryptoStreamMode.Write);
encStream.Write(data, 0, data.Length);
encStream.FlushFinalBlock();
//calculate the length of the encrypted data
byte[] bResult = new byte[ms.Position];
ms.Position = 0;
ms.Read(bResult, 0, bResult.Length) ;
rich.Text += new UnicodeEncoding().GetString(bResult) + "\n\n"; encStream.Close();
FileStream fs = new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
fs.Write(bResult, 0, bResult.Length);
fs.Close();
}
private void Decrypt_Click(object sender, System.EventArgs e)
{
FileStream fs= new FileStream(Application.StartupPath + "\\Data\\file.try", FileMode.Open, FileAccess.Read, FileShare.None);
byte[] ByteArray=new byte[fs.Length];
int nBytesRead = fs.Read(ByteArray, 0, ByteArray.Length);
fs.Close();
MemoryStream ms = new MemoryStream(ByteArray);
DES des = new DESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
MemoryStream plaintextmem = new MemoryStream();
ms.Position = 0;
do
{
//Create a byte array into which we will read the plaintext
//from CryptoStream
Byte[] buf = new Byte[100];
try
{
//read the plaintext from CryptoStream
int actualbytesread = encStream.Read(buf, 0, 100);
//if we have reached the end of stream quit the loop
if (0 == actualbytesread)
break;
//copy the plaintext byte array to MemoryStream
plaintextmem.Write(buf,0,actualbytesread);
}
catch(Exception ab)
{
MessageBox.Show(ab.Message, "Asdf");
}
}while(true);
//don't forget to close the streams
encStream.Close();
ms.Close();
//Extract the plaintext byte stream and close the MemoryStream
Byte[] plaintextbyte = plaintextmem.ToArray();
plaintextmem.Close();
//Encode the plaintext byte into Unicode string
string plaintext = new UnicodeEncoding().GetString(plaintextbyte);
encStream.Close();
}
I would appreciate any response and help from yours ~
|
|
|
|
|
Anyone here can give me some guides ? ....
|
|
|
|
|
Be patient. This isn't 24x7 support, it's a community forum.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
It helps to know what exception you're getting and what it states. And don't just give me the text from MessageBox.Show (which is not really a debugging tool - try a breakpoint or the Debug or Trace facilities at the very least), the actual exception type is good to know. Besides, in production code you'll give your user 1) a user-unfriendly message, and 2) a malicious user too much information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
In the try-catch block, this kind of error is showed when i list it out
"Data Error"
Mscorlib (in Mscorlib.dll)
at System.Security.Cryptography.CryptoAPITransform._DecryptData(IntPtr hKey, Byte[] rgb, Int32 ib, Int32 cb, Boolean fDone)
at System.Security.Cryptography.CryptoAPITransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)
at WindowsApplication3.Form1.button10_Click(Object sender, EventArgs e)
|
|
|
|
|
You're still not telling me the actual exception Type, only the message.
In any case, try using a buffer that is a power of 2 (2, 4, 8, 16, 32, 64, ...) when decrypting. This is a pretty common requirement when decrypting so that the decryption algorithm has all that it needs to decrypt a block. This usually isn't the case when you don't have a block of the right size, which is almost always a power of 2.
Also, don't new-up a UnicodeEncoding object every time you use it. Use Encoding.Unicode which create a single instance to be used any time you use it, which saves time and memory:
byte[] buffer = Encoding.Unicode.GetBytes(someString);
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Actually, there was no exception being thrown.
If you have seen the code ... the try-catch is something that i insert to see what thing is happening ...
This is because ... the program was hanged in the do loop ...
I don't know how to express ... if you have tried the code, you will know what is happening...
Things made me mad ...
Anyway, u teach me a lot and thanks for your reply ...
i still look forward you can help me to solve this problem ....
|
|
|
|
|
It not exception is being thrown, then what is being caught? Of course an exception is thrown, but you're handling it, at least that's what your code shows.
As I mentioned before, though, decryption typically expects a buffer with a size that is a power of 2 so that it has enough information to decrypt a block. If you don't provide enough information, it doesn't have enough information to decrypt a block of cipher text and would hang waiting for more, or throw an exception. Try resizing your cipher text buffer to be a power of 2 and try again.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all
Can anyone suggest me which way of string replacment is much better ??
1) string str = "Date today is " + Date.Now.ToString() + " !!!!"
Or
2) string str = String.Format("Date today is {0} !!!!",Date.Now.ToString())
Thanxs
Maharishi Bhatia
|
|
|
|
|
Never concatenate strings using + - it's very expensive since two iterations for each string and each concatenation are necessary, making it an (m-1)O(2n) operation where n is the number of character for two strings and m is the number of strings.
Use String.Concat or String.Format like you're doing, but don't use ToString . What do you thing String.Format is for? Lets say you wanted to format the date using the long date format. Just use the following:
string s = string.Format("The date is {0:D}.", DateTime.Now); If you use ToString , you loose that ability (unless you use the overload for DateTime.ToString which takes a string and optionally a different culture's IFormatProvider . I'll be submitting an article on this topic in the next couple days. You might want to keep a look out and read it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
bloody hell never knew it was expensive to use ToString() !
Well you learn something everyday
|
|
|
|
|
It isn't expensive to use ToString (unless the implementation is crappy), I said it was expensive to concat strings using + . Using ToString in the variable argument list for String.Format (and the like) is not only unnecessary but in many cases dumb since you loose the ability to apply custom formats (unless you pass the format specifiers as args to the ToString overload for the IFormattable implementation).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
Never concatenate strings using + ...Use String.Concat
C# code that 'pluses' strings together gets IL compiled down to .Concat, right? Maybe I'm misunderstanding you.
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
For the Microsoft C# compiler, yes, and under certain circumstances - which typically warrant String.Format for better control. Depending on the operands you're passing, the compiler doesn't always optimize using String.Concat either.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Well then, if you take into account that it's almost always optimized into String.Concat , then wouldn't you say that using + is actually much quicker than using String.Format , which involves parsing and appending?
From a readibility stand point, the + operator is superior in my opinion. Now, if special formatting is necessary, then the Format method is available -- for formatting purposes.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
It depends on the circumstances, I'd say. Like you said, for special formatting using String.Format is definitely better, though you could accomplish this task with + or String.Concat if you use the overloaded ToString that corresponds to the IFormattable implementation.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
3) string str = "Date today is " + Date.Now + " !!!!";
ToString() is automatically called for you.
Regards,
Alvaro
Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.
|
|
|
|
|
how do i copy files to the clipboard with csharp so that i can right click a dir and past them into it (i really need to be able to past bitmap into a program but its the same principle)
chad
|
|
|
|
|
Read about the Clipboard class in the .NET Framework SDK. You use it in a similar fashion as you do drag and drop. You fill a DataObject with data and add it to the clipboard, only the DragDropEffects is not used.
To add it:
DataObject do = new DataObject(DataFormats.FileDrop,
@"C:\temp\file.txt");
Clipboard.SetDataObject(do, true); To get it:
DataObject do = Clipboard.GetDataObject();
if (do.GetDataPresent(DataFormats.FileDrop))
{
string filename = (string)do.GetData(DataFormats.FileDrop);
File.Copy(filename, destination);
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|