fifo123
asked on
Editting a file programatically
Hey guys,
I am working on an application that needs to open a file that has only one line, encrypt the content, and save it. I don't want to create a new file; just want to open the file, make my changes and save it. I am using the objects "streamreader" and "streamwriter" to accomplish this. The problem I'm having is that when I decrypt the file might be somewhat corrupted. My encryption code is very stable and works great(we use it for all our applications). I am thinking it could be the way I'm writing to the file that is creating the problem. Here is the code snippet:
string sLine = string.empty;
string sEncrypt = string.empty;
// Read the line from the file and encrypt it
using(StreamReader myReader = new StreamReader(@"c:\test.txt "))
{
while((sLine = myReader.ReadLine()) != null)
{
// encrypt the message
sEncrypt = myComponent.Encrypt(sLine) ;
}
}
// Write the encrypted line back to the file
using(StreamWriter myWriter = new StreamWriter(@"c:\test.txt "))
{
myWriter.WriteLine(sEncryp t );
myWriter.Flush();
myWriter.Close();
}
Is there another way I can try to manipulate the files. Any suggestions appreciated.
I am working on an application that needs to open a file that has only one line, encrypt the content, and save it. I don't want to create a new file; just want to open the file, make my changes and save it. I am using the objects "streamreader" and "streamwriter" to accomplish this. The problem I'm having is that when I decrypt the file might be somewhat corrupted. My encryption code is very stable and works great(we use it for all our applications). I am thinking it could be the way I'm writing to the file that is creating the problem. Here is the code snippet:
string sLine = string.empty;
string sEncrypt = string.empty;
// Read the line from the file and encrypt it
using(StreamReader myReader = new StreamReader(@"c:\test.txt
{
while((sLine = myReader.ReadLine()) != null)
{
// encrypt the message
sEncrypt = myComponent.Encrypt(sLine)
}
}
// Write the encrypted line back to the file
using(StreamWriter myWriter = new StreamWriter(@"c:\test.txt
{
myWriter.WriteLine(sEncryp
myWriter.Flush();
myWriter.Close();
}
Is there another way I can try to manipulate the files. Any suggestions appreciated.
Are you closing the StreamReader before writing to it with StreamWriter?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.