I have a method that generates some information. As I was in a hurry to deliver, I did and I sent. Well, it turns out, that looking at the code more carefully, I realized how crappy it is. It was made to meet a specification and as it was changed it had to change the code. The current rule is as follows:
I have a method that handles files from FarmInterna . The same goes for FarmExterna . It turns out that I now need to create two folders folders before FarmInterna folder. Homologation and Production . In each folder ( Homologacao e Producao ) there should be two folders: FarmInterna and FarmExterna . I'm going to talk only about FarmInterna, which will also be valid for Externa. The same files that I generate in path: ...Homolagacao/FarmInterna/web/...
are the same for: ...Producao/FarmInterna/web/...
. This made my code extremely ugly and inelegant. How do I not to duplicate the code as it is? Is there any pattern for this? Below my code. Where there is the H letter refers to Homolagation and
production.
private void CriaPastaFarmInterna()
{
string novo_path_H = caminho_original + @"\Destino\Temp\Homologacao\FarmInterna\web";
string novo_path_P = caminho_original + @"\Destino\Temp\Producao\FarmInterna\web";
string path_files = caminho_original + @"\Destino\Temp";
DirectoryInfo dirInfoH = new DirectoryInfo(novo_path_H);
DirectoryInfo dirInfoP = new DirectoryInfo(novo_path_P);
int indice = 1;
if (dirInfoH.Exists == false)
Directory.CreateDirectory(novo_path_H);
if (dirInfoP.Exists == false)
Directory.CreateDirectory(novo_path_P);
List<String> myFiles = Directory.GetFiles(caminho_original + @"\Destino\Temp\web", "*.*", SearchOption.AllDirectories).ToList();
List<String> myDirectoriesH = Directory.GetDirectories(caminho_original + @"\Destino\Temp\web").ToList();
List<String> myDirectoriesP = Directory.GetDirectories(caminho_original + @"\Destino\Temp\web").ToList();
var diretorios_H = myDirectoriesH.Where(d => !d.Contains("FarmInterna"));
var diretorios_P = myDirectoriesP.Where(d => !d.Contains("FarmInterna"));
try
{
foreach (string file in myFiles)
{
FileInfo mFile = new FileInfo(file);
string newFileH = novo_path_H + (file.Replace(caminho_original + @"\Destino\Temp\web", ""));
string newFileP = novo_path_P + (file.Replace(caminho_original + @"\Destino\Temp\web", ""));
if (!Directory.Exists(newFileH))
Directory.CreateDirectory(Path.GetDirectoryName(newFileH));
if (!Directory.Exists(newFileP))
Directory.CreateDirectory(Path.GetDirectoryName(newFileP));
if (new FileInfo(newFileH).Exists == false)
mFile.CopyTo(newFileH);
if (new FileInfo(newFileP).Exists == false)
mFile.CopyTo(newFileP);
}
RenomearWebConfig(novo_path_H, "H");
RenomearWebConfig(novo_path_P, "P");
}
catch(Exception ex)
{}
}