Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I never asked for anything, and this is pretty simple.
But I can't get it working somehow.

C#
private void keyPress(object sender, KeyPressEventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"C:\Users\Public\Documents\Test.txt");
            switch (e.KeyChar)
            {
                case 'a':
                    sw.Write("a");
                    sw.Flush();
                    return;
            }
        }


I bet someone knows how to get the sw to write into my .txt when ever i press the key 'a'.

Thanks, XauS.
Posted

Hi my friend. Because Stream is disposable. you must Close them after manipulate them.
this code works for me correctly:
C#
switch (e.KeyChar)
            {
                case'a':
                    stream.Write(e.KeyChar);
                    stream.Close();
                    break;
            }

another important thing is using return here. why? tell me.
after case you must use break.
good luck.
 
Share this answer
 
v2
Comments
XauS_ 24-Jul-13 7:36am    
Thanks for you answere, but no luck here either. As I said to @Sarin_VT his code looks, like this good. But the solutions are not working. I'm starting to think that it would be some other problems, like 'admin rights' or what so ever.
Try this code,

C#
private void keyPress(object sender, KeyPressEventArgs e)
        {
            try
            {

            StreamWriter sw = new StreamWriter
                           (@"C:\Users\Public\Documents\Test.txt");
            switch (e.KeyChar)
            {
                case 'a':
                    { // you haven't put this braces
                    sw.Write("a");
                    sw.Flush();
                    sw.close();
                    };
                    break; //you put return
            }
            }
           catch(Exception e)
           {
              //catch statements
           }
        }
 
Share this answer
 
v4
Comments
XauS_ 24-Jul-13 7:24am    
@Sarin VT
You code gets the SW to work. But that does not solve my problem whenever I press the key 'a' to get SW to write the letter 'a' into my .txt file thu.
Sarin VT 24-Jul-13 7:26am    
I have edited the solution
Sarin VT 24-Jul-13 7:32am    
You have done few mistakes, i think i have pointed out all,
Is it working now??
XauS_ 24-Jul-13 7:34am    
I have tried, the code looks good. But I can't get that exact code to work. Sorry.. :/
Sarin VT 24-Jul-13 7:37am    
Are you sure you made all the modifications,
1. braces
2. sw.close()
3. break;
4. put a try catch // incase any exception occures
use this code to write text into text file

private void keyPress(object sender, KeyPressEventArgs e)
{
//StreamWriter sw = new StreamWriter(@"D:\Test.txt");
switch (e.KeyChar)
{
case 'a':
File.AppendAllText(@"C:\Users\Public\Documents\Test.txt", "a");

return;
}
}
 
Share this answer
 
Comments
XauS_ 24-Jul-13 8:21am    
I got that part to work. But now there is a problem with getting the code to work outside the form (as the form is hidden).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900