Background: An application that opens a file, searches the file for the old password, replace the old password, and then saves an closes the file. The file is used by another process.
Servers
I have two environments one for dev and prd.
The Issue
* On prod the files are opened and saved with the current date and time, but the previous password is not replaced.
* On dev the files are opened, replaced, and saved correctly with the new password.
Things I Have Tried
* Check file permissions between the two environment and found that it was not an issue
* Implemented an error system to see if there is an error message. none are being returned
* Implemented a check to see if the file are out of sync the log and password in the file
What I think it is
There are three major differences between the two environments
* MSXML 6 Service Pack 2 (KB973686) 6.20.2003.0
* Security Update for Microsoft .NET Framework 3.5 SP1 (KB2416473) 1
* Update for Microsoft .NET Framework 3.5 SP1 (KB963707) 1
If you can help me, that would be sweet. I provided the code that does the replace below.
If i need to make something more clear please let me know.
string passwordLog = _appDrive + @"Apps\FTP\Password\Password_Log.txt";
StreamReader sr = new StreamReader(file);
StreamReader pl = new StreamReader(passwordLog);
//read the entire file at once
string contents = sr.ReadToEnd();
string oldContents = contents;
string contents2 = pl.ReadToEnd();
//close up file
sr.Close();
pl.Close();
//use regular expressions to search and replace our text
//added to search if previous password is found in content
bool search1 = Regex.IsMatch(contents,searchFor);
//searches for previous password in log
bool search2 = Regex.IsMatch(contents2, searchFor);
string newContents = null;
if (search1)
{
WriteProcessLog("Password was found in file stream" + file);
WriteProcessLog("serachFor:" + searchFor + "| replaceWith:" + replaceWith);
newContents = Regex.Replace(contents, searchFor, replaceWith, RegexOptions.None);
}
else
{
//the will update files that are out of sync
if (search2)
{
string[] words = contents.Split(' ');
bool checkNext = false;
int i=0;
WriteProcessLog("Password was found in file password log");
foreach (string w in words)
{
if (checkNext)
{
//checkNext = false;
//sets the password in file to searchFor if out of sync
searchFor = w.Trim();
WriteProcessLog("Password in file:" + searchFor);
break;
}
if (w == "-p")
{
i++;
}
if(w == "-p" && i == 2 )
{
checkNext = true;
}
}
}
//searches the file for the new searchFor
search1 = Regex.IsMatch(contents2, searchFor);//search log for password in log
if (search1)
{
//if new searchFor is found then updates the file
var regex = new Regex(searchFor, RegexOptions.Compiled);
Match match = regex.Match(searchFor);
if (!match.Success)
{
foreach (var currentMatch in regex.Matches(contents))
{
newContents = Regex.Replace(contents, searchFor, replaceWith, RegexOptions.None);
}
}
else
{
newContents = oldContents;
}
WriteProcessLog("serachFor:" + searchFor + " | replaceWith:" + replaceWith);
}
else
{
WriteProcessLog("Not found in password log | serachFor:" + searchFor + " | in file" + file);
ChangeStatus("Fail", "Files Are Out of Sync. " + file);
throw new Exception("Files Are Out of Sync. " + file);
}
}
//get a StreamWriter for writing the new text to the file
using (StreamWriter sw = File.CreateText(file))
{
//write the contents
try
{
sw.Write(newContents);
WriteProcessLog("Writing new contents");
sw.Close();
}
catch (Exception writeEx)
{
ChangeStatus("Fail", writeEx.Message);
throw new Exception(writeEx.Message);
}
}