0

My C# code call git and cloning repository, but when my program ends i need to delete this folder with clone repo inside. When i use

Directory.Delete(path, true)

All files are deleted except .git and when program trying to delete .git -

"Access to the path 'pack-3241a6c3ea7ff447e5ba864f5d87ef66c5913670.idx' is denied."

I tried

Directory.Delete doesn't work. Access denied error but under Windows Explorer it's ok

But it won't help.

How can i solve it?

1 Answers1

4

when clone source from gitlab, in that folder will contain files is readonly and hidden. So if you use Directory.delete() file is read only and hidden can not delete before, folder will no delete. To delete it, you should find all file(read only, hidden, ...) in the directory, and then set attribute is normal. now you can delete all file and then is folder.

"path" you should use "<disk>:\<yourfoler>\" format. example: "C:\gitlab\"

var directory = new DirectoryInfo(path) { Attributes = FileAttributes.Normal };

foreach (var info in directory.GetFileSystemInfos("*", SearchOption.AllDirectories))
{
    info.Attributes = FileAttributes.Normal;
}

directory.Delete(true);
denis bui
  • 65
  • 7