【问题标题】:C#. How to delete git folder?C#。如何删除 git 文件夹?
【发布时间】:2020-08-17 10:48:11
【问题描述】:

我的 C# 代码调用 git 和克隆存储库,但是当我的程序结束时,我需要删除这个包含克隆存储库的文件夹。 当我使用

Directory.Delete(path, true)

所有文件都被删除,除了 .git 和程序试图删除 .git -

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

我试过了

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

但这无济于事。

我该如何解决?

【问题讨论】:

  • 你启动的git进程有没有可能还在运行?
  • @AluanHaddad 我已经注释掉除了删除之外的所有行
  • 我很困惑。也许显示更多的代码。此外,您可能有一个崩溃的 git.exe 由之前仍然存在的调试会话启动。
  • @AluanHaddad 我现在的代码中只有 Directory.Delete(@"C:/Test", true) 。 git.exe 没有启动,我有管理员权限。我可以在资源管理器中毫无问题地删除它
  • Directory.Delete 不会删除只读文件。

标签: c#


【解决方案1】:

当从 gitlab 克隆源时,该文件夹中将包含只读和隐藏文件。所以如果你使用 Directory.delete() 文件是只读且隐藏之前不能删除,文件夹不会被删除。 要删除它,你应该在目录中找到所有文件(只读,隐藏,...),然后设置属性是正常的。现在您可以删除所有文件,然后是文件夹。

"path" 你应该使用 ":\\" 格式。例如:“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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2012-08-16
    相关资源
    最近更新 更多