Gáb*_*kos 6 c# git github azure-worker-roles libgit2sharp
我想删除使用LibGit2Sharp从远程存储库克隆的本地repo文件夹.我在这里读到,我必须在删除它之前Dispose()存储库,但它仍然无法正常工作.
using (var repo = new LibGit2Sharp.Repository(path))
{
repo.Dispose();
}
Directory.DeleteFolder(path);
Run Code Online (Sandbox Code Playgroud)
我还有一个例外:
Access to the path 'c16566a7-202a-4c8a-84de-3e3caadd5af9' is denied.
Run Code Online (Sandbox Code Playgroud)
'path'变量的内容如下:
C:\Users\USERNAME\AppData\Local\dftmp\Resources\c16566a7-202a-4c8a-84de-3e3caadd5af9\directory\UserRepos\github.com\domonkosgabor\testrepo
Run Code Online (Sandbox Code Playgroud)
此文件夹由辅助角色创建到本地存储.
我该怎么做才能删除整个文件夹(包括.git)?
AJ *_*son 23
为了其他任何有此问题的人的利益:
我有同样的问题,但UnauthorizedAccessException
即使我以管理员身份运行,我仍然得到了一个,我正确地处理了存储库对象.事实证明文件.git
夹中的某些文件被标记为ReadOnly
,因此我必须遍历每个文件并删除ReadOnly
之前删除该属性.我写了一个自定义方法来做到这一点:
/// <summary>
/// Recursively deletes a directory as well as any subdirectories and files. If the files are read-only, they are flagged as normal and then deleted.
/// </summary>
/// <param name="directory">The name of the directory to remove.</param>
public static void DeleteReadOnlyDirectory(string directory)
{
foreach (var subdirectory in Directory.EnumerateDirectories(directory))
{
DeleteReadOnlyDirectory(subdirectory);
}
foreach (var fileName in Directory.EnumerateFiles(directory))
{
var fileInfo = new FileInfo(fileName);
fileInfo.Attributes = FileAttributes.Normal;
fileInfo.Delete();
}
Directory.Delete(directory);
}
Run Code Online (Sandbox Code Playgroud)
我想删除使用 LibGit2Sharp 从远程存储库克隆的本地存储库文件夹。我在这里读到,我必须先 Dispose() 存储库,然后才能删除它。
LibGit2Sharp 保留 .git 文件夹中的一些文件(主要是出于性能原因的包文件)。调用Dispose()
将释放这些句柄并释放非托管内存。
因此,确实强烈建议依赖该using
语句(或者,至少Dispose()
在完成后依赖于 Repository 实例)。
如果您不这样做,当您的 AppDomain 卸载时,这些句柄最终将通过终结器释放,但您将无法真正控制“何时”发生这种情况。
编辑:再次阅读你的代码,我忽略了一些事情。推荐的模式是
using (var repo = new LibGit2Sharp.Repository(path))
{
// Do amazing stuff
}
Run Code Online (Sandbox Code Playgroud)
或者
var repo = new LibGit2Sharp.Repository(path);
// Do amazing stuff
repo.Dispose();
Run Code Online (Sandbox Code Playgroud)
事实上,一旦代码到达作用域末尾,该using
语句就会自动发出调用。Dispose()
对路径“c16566a7-202a-4c8a-84de-3e3caadd5af9”的访问被拒绝。
关于这一点,我认为这与LibGit2Sharp无关。
进程(尝试删除以 guid 命名的文件夹)是否在被授予足够权限的身份下运行?
归档时间: |
|
查看次数: |
1847 次 |
最近记录: |