10

I have an application that is looking through some files for old data. In order to make sure we don't corrupt good projects, I'm copying the files to a temporary location. Some of the directories I'm checking are source-code directories, and they have .svn folders. We use Subversion to manage our code.

Once I've searched through all of the files, I want to delete the temp cache. Sounds easy, right?

For some reason, all of my .svn directories won't delete from the cache. They crash the app.

For reasons (too deep to go into here), I have to use the temp folder, so just "scan the original file" is out of the question for political reasons.

I can go into explorer and delete them. No problem. No warnings. Just deletes. But the code crashes with "Access to {file} is denied." I'm at my wits end with this one, so any help would be appreciated.

While I've simplified the function a LITTLE for sake of your sanity, the code REALLY is about this simple.

List<string> tmpCacheManifest = new List<string>();
string oldRootPath = "C:\\some\\known\\directory\\";
string tempPath = "C:\\temp\\cache\\";

foreach (string file in ListOfFilesToScan)
{
    string newFile = file.Replace(oldRootPath, tempPath);

    // This works just fine.
    File.Copy(file, newFile);

    tmpCacheManifest.add(newFile);
}

//    ... do some stuff to the cache to verify what I need.


// Okay.. I'm done.. Delete the cache.
foreach (string file in tmpCacheManifest)
{
   // CRASH!
   File.Delete(file);
}

* Update *: The exception is UnauthorizedAccessException. The text is "Access to the path 'C:\temp\cache\some-sub-dirs\.svn\entries' is denied."

It happens under XP, XP-Pro and Windows 7.

* Update 2 * None of my validation even ATTEMPTS to look at subversion files. I do need them, however. That's part of the political crap. I have to show that EVERY file was copied... wheter it was scanned or not.

And I realize what the usual suspects are for File.Delete. I realize what UnauthorizedAccessException means. I don't have access. That's a no-brainer. But I just copied the file. How can I NOT have access to the file?

* Update 3 * The answer was in the "read-only" flag. Here's the code I used to fix it:

    foreach (string file in ListOfFilesToScan)
{
    string newFile = file.Replace(oldRootPath, tempPath);

    // This works just fine.
    File.Copy(file, newFile);

    //// NEW CODE ////
    // Clear any "Read-Only" flags
    FileInfo fi3 = new FileInfo(fn);
    if ((fi3.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
    {
        fi3.Attributes = (FileAttributes)(Convert.ToInt32(fi3.Attributes) - Convert.ToInt32(FileAttributes.ReadOnly));
    }



    tmpCacheManifest.add(newFile);
}

//    ... do some stuff to the cache to verify what I need.

4

7 回答 7

12

据我所知,Subversion 将其 .svn 子目录中的文件标记为只读。

在删除文件之前尝试重置只读属性。我真的不知道任何 C#,但一个快速的谷歌建议这可能会奏效:

File.SetAttributes(file, FileAttributes.Normal);
于 2010-01-05T16:33:17.717 回答
2

我看到的唯一问题是这部分:

// ... do some stuff to the cache to verify what I need.

如果您确实打开了文件而忘记关闭它,您仍然可以独占访问它,因此以后无法删除它。

于 2010-01-05T16:25:04.740 回答
2

听起来您无权删除文件...

system.io.file.delete

上面的链接说你什么UnauthorizedAccessException时候得到:

调用者没有所需的权限。

-或者-

路径是一个目录。

-或者-

path 指定了一个只读文件。

它就是其中之一。

于 2010-01-05T16:25:53.817 回答
1

听起来像是权限问题。棘手的一个,因为如果 File.Copy 已经工作,你显然有写访问权限....

我唯一能想到的是该文件仍然在某处打开了一个句柄(正如其他人所建议的那样,也许在您对缓存部分做一些事情时)。

于 2010-01-05T16:28:49.020 回答
0

首先:“Crash”意味着一个例外,对吧?哪一个?你能抓住它并展示它吗?

第二件事:您正在复制颠覆存储库,尽管您不关心颠覆元数据?这就是 svn export 的意义所在(目标中没有 .svn 目录)。

第一个问题的答案是你真正需要提供的。也许有些东西抓住了 .svn 并锁定了一些文件。TortoiseSVN 也许(给你漂亮的覆盖图标..)?

于 2010-01-05T16:23:03.840 回答
0

如果文件夹包含只读文件, Directory.Delete 不会删除它并引发您遇到的异常。对于此页面的未来访问者,我找到了一个简单的解决方案,它不需要我们递归所有文件并更改它们的只读属性:

Process.Start("cmd.exe", "/c " + @"rmdir /s/q C:\Test\TestDirectoryContainingReadOnlyFiles"); 

(稍微改变一下不要立即触发 cmd 窗口,这在整个互联网上都可用)

于 2012-06-02T06:04:20.463 回答
-2

Not understanding what you want to do so much, but what about chmoding it to 777 or 775. :-/

Edit:

Noticed your on windows. You'd have to change the permissions. Don't know how windows does that :-/

于 2010-01-05T16:20:43.417 回答