-1
I’m having a hard time creating a folder inside another one and moving the content into that new folder. The rule is:
I unzip a file
.zip
inside that directory:C:\Teste_Zip\Destino_Teste\Temp/>
. Well if there is a folderweb
, then my business starts.Inside the folder
web
, I must create two folders:FarmInterna
andFarmExterna
. Everything inside the briefcaseweb
, must be moved to the folderFarmInterna
and the new folderFarmExterna
some files will be copied, based on another rule.
Well, the whole point is that when creating the new folder, I can’t move the files and the folder gets deleted and I don’t know why.
Look at my code:
private void CriaPastaFarmInterna()
{
string path_trabalho = ConfigurationManager.AppSettings["Target_Dir_Temp"];
string novo_path = path_trabalho + "\\FarmInterna";
DirectoryInfo dirInfo = new DirectoryInfo(novo_path);
if (dirInfo.Exists == false)
Directory.CreateDirectory(novo_path);
List<String> myFiles = Directory
.GetFiles(path_trabalho, "*.*", SearchOption.AllDirectories).ToList();
foreach (string file in myFiles)
{
FileInfo mFile = new FileInfo(file);
// to remove name collusion
if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false)
mFile.MoveTo(dirInfo + "\\" + mFile.Name);
}
}
The need to create and move to the FarmInterna
.
Improving the question, the folder FarInterna
must be inside the web folder and the folders and files that must be moved to that folder, are only the folders and files that are inside the web folder, except the folder FarmInterna
, it is obvious.
I redid my code and when it comes to foreach, it gives a moveTo()
, He says he couldn’t find part of the way. That is, it enters with the correct path, but since the internal Farminternal folder is empty, that must be it, there is no folder. Below the new code.
private void CriaPastaFarmInterna()
{
string path_trabalho = ConfigurationManager.AppSettings["Target_Dir_Temp"] + @"\web";
string novo_path = path_trabalho + @"\FarmInterna";
DirectoryInfo dirInfo = new DirectoryInfo(novo_path);
if (dirInfo.Exists == false)
Directory.CreateDirectory(novo_path);
List<String> myFiles = Directory.GetFiles(path_trabalho, "*.*", SearchOption.AllDirectories).ToList();
List<String> myDirectories = Directory.GetDirectories(path_trabalho).ToList();
var diretorios = myDirectories.Where(d => !d.Contains("FarmInterna"));
foreach (var di in diretorios)
{
foreach (string file in myFiles)
{
FileInfo mFile = new FileInfo(file);
string newFile = novo_path + (file.Replace(path_trabalho, ""));
//string newFile = novo_path + "\\" + (file.Replace(path_trabalho, ""));
if (new FileInfo(newFile).Exists == false)
mFile.MoveTo(newFile);
}
}
}
There is nothing in your code that would cause the 'Farminterna' folder to be deleted. However, there are some inconsistencies in your code: 1. The method
GetFiles
lists only files and not directories. To list everything from the folder, you would also need to use the methodGetDirectories
. 2. When listing directories also, your folderFarmInterna
would enter the list of what should be copied - you should add a check so that it is not moved as well.– Marcus Vinicius
@Marcusvinicius, my question is as follows. The Farminterna folder should be inside the web folder and when I do this, it gives me the message that is already being used and does nothing else.
– pnet