Click to See Complete Forum and Search --> : [RESOLVED] Opening a notepad file
brooke
Mar 4th, 2006, 11:52 PM
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?
deranged
Mar 5th, 2006, 12:03 AM
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?
brooke
Mar 5th, 2006, 12:05 AM
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.
deranged
Mar 5th, 2006, 12:09 AM
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.
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:
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.
brooke
Mar 5th, 2006, 12:11 AM
You mean it will delete any NOTEPAD file that exists with that name, right?
deranged
Mar 5th, 2006, 12:24 AM
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.
brooke
Mar 5th, 2006, 12:32 AM
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).
deranged
Mar 5th, 2006, 01:15 AM
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.
Kasracer
Mar 5th, 2006, 01:45 PM
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.
using (System.IO.StreamWriter fout = new System.IO.StreamWriter("My TextFile.txt"))
{
fout.Write(MyString);
}
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.
brooke
Mar 5th, 2006, 01:56 PM
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.
using (System.IO.StreamWriter fout = new System.IO.StreamWriter("My TextFile.txt"))
{
fout.Write(MyString);
}
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!
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.