:20:S10734539/940
am reading swift messages from a folder, and at line :20: I perform some sort of validation to know if the file and reading is valid. In this case if line :20: has 940 in it, the windows service reads and moves it to a success folder when done. But an invalid file will not have 940 on line :20: , the windows service is meant to move the invalid file to an invalid file location. I have written a code to do that, but it not able to move the file. I get an error message 'file in use' find below snippets of my code.
if (Directory.Exists(@CBN_INFLOW940))
DirectoryInfo dr = new DirectoryInfo(CBN_INFLOW940);
FileInfo[] fi = dr.GetFiles();
string[] files = Directory.GetFiles(CBN_INFLOW940);
int lengthchk = 0;
if (files.Length < 1)
check = false;
while (files.Length > lengthchk)
{
StringBuilder sb = new StringBuilder();
logger.Info(fi[lengthchk].Name + ": read from folder");
string narrationgen = "";
bool isvalidrtgs = false;
DateTime createddate = fi[lengthchk].CreationTime.Date;
FileStream stream = null;
try
{
stream = File.Open(files[lengthchk], FileMode.Open, FileAccess.ReadWrite, FileShare.None);
StreamReader sf = new StreamReader(stream);
}
and
if (line.Contains(":20:"))
{
firstchk = 1;
if (!line.Contains('/'))
{
string[] fnamesplit = fi[lengthchk].Name.Split('.');
string newfname = fnamesplit[0] + "_bk" + ".txt";
string destlocation = Invalidfilelocation940 + newfname;
string sourcelocation = CBN_INFLOW940 + fi[lengthchk]; // + "\\"
File.Move(sourcelocation, destlocation);
return;
}
string[] narr = line.Split('/');
string filecode = narr[1];
if (filecode.Trim() != "940")
{
string[] fnamesplit = fi[lengthchk].Name.Split('.');
string newfname = fnamesplit[0] + "_bk" + ".txt";
string destlocation = Invalidfilelocation940 + newfname;
string sourcelocation = CBN_INFLOW940 + "\\" + fi[lengthchk];
File.Move(sourcelocation, destlocation);
return;
}
}
Try to split out your check code from your move code. It will be a better design, plus you may be locking the file for reading while you are trying to move it.
Think:
if ( ShouldMoveFile( filename ) )
{
File.Move...
}
And make sure you close the file you are reading.
If you keep the code the same:
add the lines:
sf.Close();
stream.Close();
before you go to move the files.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments