I have a service that monitors failure and success directories for files that are generated by an engineering program. I want to check the change was in the success or failure folder, and if it was a new file creation. When those criteria are met I need to check if the file type matches one of the keys for a dictionary, and if it does I want to copy that file to a destination as declared in the config file.
I have an if
statement that says:
if (NormalizePath(Path.GetDirectoryName(e.FullPath)) == NormalizePath(this.successFolder)
&& e.ChangeType == WatcherChangeTypes.Created)
{
string fileExtension = Path.GetExtension(e.FullPath);
try
{
//if the file type is desired copy to destinations.
if( this.successDictionary.ContainsKey(fileExtension) )
{
CopyToDestination(this.successDictionary[fileExtension], e);
}
}
catch (IOException ioe)
{
activeLog.WriteLineToLog("FolderMonitor IOException: " + ioe.Message);
}
}
//if a file was added to the fail folder
else if (NormalizePath(Path.GetDirectoryName(e.FullPath)) == NormalizePath(this.failFolder)
&& e.ChangeType == WatcherChangeTypes.Created)
{
activeLog.WriteLineToLog(e.FullPath + " has been added to the fail folder.");
string fileExtension = Path.GetExtension(e.FullPath);
try
{
//if the file type is desired copy to destinations.
if (this.failDictionary.ContainsKey(fileExtension))
{
CopyToDestination(this.failDictionary[fileExtension], e);
}
}
catch (IOException ioe)
{
activeLog.WriteLineToLog("FolderMonitor IOException: " + ioe.Message);
}
}
Is it better to do it the first way or the second way?
string fileExtension = Path.GetExtension(e.FullPath);
if (NormalizePath(Path.GetDirectoryName(e.FullPath)) == NormalizePath(this.successFolder)
&& e.ChangeType == WatcherChangeTypes.Created)
{
try
{
//if the file type is desired copy to destinations.
if( this.successDictionary.ContainsKey(fileExtension) )
{
CopyToDestination(this.successDictionary[fileExtension], e);
}
}
catch (IOException ioe)
{
activeLog.WriteLineToLog("FolderMonitor IOException: " + ioe.Message);
}
}
//if a file was added to the fail folder
else if (NormalizePath(Path.GetDirectoryName(e.FullPath)) == NormalizePath(this.failFolder)
&& e.ChangeType == WatcherChangeTypes.Created)
{
activeLog.WriteLineToLog(e.FullPath + " has been added to the fail folder.");
try
{
//if the file type is desired copy to destinations.
if (this.failDictionary.ContainsKey(fileExtension))
{
CopyToDestination(this.failDictionary[fileExtension], e);
}
}
catch (IOException ioe)
{
activeLog.WriteLineToLog("FolderMonitor IOException: " + ioe.Message);
}
}
It's really just a question of if it is better to sometimes waste a bit of memory, or if it is better to have it more optimized. I understand that in the grand scheme of things it's rather minimal how much memory it uses, but the voices in my head can't decide which one is better.