Results 1 to 10 of 10

Thread: [RESOLVED] Opening a notepad file

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    MS
    Posts
    7

    Resolved [RESOLVED] Opening a notepad file

    I have created a program in C# that takes a few registry keys (Processor Identifier, Vendor Identifier, Bios Status, Bios Date, and System Identifier) and places the information into a command prompt window. What I want to do is place them into a notepad file. How can I go about having the program open notepad and having the information placed in there?

  2. #2
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Opening a notepad file

    Are you wanting to actually open notepad, and have that data put into notepad, or would you just like to save the data into a .txt file?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    MS
    Posts
    7

    Re: Opening a notepad file

    Quote Originally Posted by deranged
    Are you wanting to actually open notepad, and have that data put into notepad, or would you just like to save the data into a .txt file?
    Well, both.

    I'm actually making two programs. I want the first one to actually open notepad on the user's screen and display this information. I want the other program just to save the information in a .txt file and have it emailed to an address.

  4. #4
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Opening a notepad file

    Here's a function I wrote a few days ago to write info to a file. It will delete the file you're trying to write to if it exists so watch out. There are ways to modify it to simply append.
    Code:
            private string filewrite(string strPath, string strTextToWrite)
            {
                try
                {
                    if (File.Exists(strPath)) //make sure the file doesn't exist
                        File.Delete(strPath); //if it does, delete it.
                    FileStream fs = File.Open(strPath, FileMode.Create); //create our file
                    Byte[] info = new UTF8Encoding(true).GetBytes(strTextToWrite); //set up something to be written
    
                    fs.Write(info, 0, info.Length); //write it
                    fs.Close(); //close the file
                    return "Success!"; //return
    
                }
                catch (Exception e)
                {
                    return e.Message.ToString(); //return any errors given.
                }
    
            }
    Here is an example of how to call it:
    Code:
    Console.WriteLine(filewrite(strFilename, strInformationToWrite));
    Just make sure that if you use variables in the function to declare them and give them values before calling the function.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    MS
    Posts
    7

    Re: Opening a notepad file

    You mean it will delete any NOTEPAD file that exists with that name, right?

  6. #6
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Opening a notepad file

    any file. Notepad is just a program that can open any file and display its contents in the window. Try opening images in notepad. It won't make any sense to you (and it shouldn't), but it can be done. .txt files are just the default extention for plaintext files (As opposed to rich text files which would have an extention of .doc, .rtf, or a couple other possibilities). If you put a .dll filename in that function, it will open the .dll file (or create it if it doesn't exist) and write to it the info you told it to. No programs will recognize the dll as a library of any sort, and if you open it in notepad, you'll see the info you were supposed to write to it.

    You can make it write to a file of any extention. if you want it to write to a file named systeminfo.brooke, you can. Windows will not know how to open the file until you tell it to, and if you tell it to open it in notepad as default, you'll have no problem viewing the file's contents with notepad by simply double clicking it.

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    MS
    Posts
    7

    Re: Opening a notepad file

    Quote Originally Posted by deranged
    any file. Notepad is just a program that can open any file and display its contents in the window. Try opening images in notepad. It won't make any sense to you (and it shouldn't), but it can be done. .txt files are just the default extention for plaintext files (As opposed to rich text files which would have an extention of .doc, .rtf, or a couple other possibilities). If you put a .dll filename in that function, it will open the .dll file (or create it if it doesn't exist) and write to it the info you told it to. No programs will recognize the dll as a library of any sort, and if you open it in notepad, you'll see the info you were supposed to write to it.

    You can make it write to a file of any extention. if you want it to write to a file named systeminfo.brooke, you can. Windows will not know how to open the file until you tell it to, and if you tell it to open it in notepad as default, you'll have no problem viewing the file's contents with notepad by simply double clicking it.
    I think I get what you're saying.
    You just worried me a bit when you said it would delete it.
    So if I were to set the program to create a program called systeminfo.kitty (or whatever) to place the information into, I wouldn't have to worry about anything getting deleted? (As long as I set the .kitty files to open in notepad on my computer, of course).

  8. #8
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: Opening a notepad file

    Well, when you call the function, the first arguement is the full filename to the file.

    For simplicity, we'll use "C:\file.dat". When the function is called, it will see if the file exists, if it does, it will delete it, then soon after that, re-create the file and write info to it. The file specified is the only file that is in danger of being deleted.

  9. #9
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: Opening a notepad file

    Just use a StreamWriter, throw the data out into the Text file and you're done. To open it, just do a System.Diagnostics.Process.Start("MyTextfile.txt"); and it'll open it in the default text editor (most likely Notepad but it may not be as well).

    There is no point in using a Filestream and converting the data into bytes when the StreamWriter can take in the Text directly and write it.

    VB Code:
    1. using (System.IO.StreamWriter fout = new System.IO.StreamWriter("My TextFile.txt"))
    2.             {
    3.                 fout.Write(MyString);
    4.             }

    That's all you need to do. The using statement will ensure the StreamWriter is closed and disposed of immediately when it goes out of scope.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  10. #10

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Location
    MS
    Posts
    7

    Re: Opening a notepad file

    Quote Originally Posted by kasracer
    Just use a StreamWriter, throw the data out into the Text file and you're done. To open it, just do a System.Diagnostics.Process.Start("MyTextfile.txt"); and it'll open it in the default text editor (most likely Notepad but it may not be as well).

    There is no point in using a Filestream and converting the data into bytes when the StreamWriter can take in the Text directly and write it.

    VB Code:
    1. using (System.IO.StreamWriter fout = new System.IO.StreamWriter("My TextFile.txt"))
    2.             {
    3.                 fout.Write(MyString);
    4.             }

    That's all you need to do. The using statement will ensure the StreamWriter is closed and disposed of immediately when it goes out of scope.
    Got it! Thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width