新创建的文件上出现 UnauthorizedAccessException

发布于 2024-08-16 20:34:08 字数 2125 浏览 3 评论 0原文

我有一个应用程序正在查找一些文件以查找旧数据。为了确保我们不会破坏好的项目,我将文件复制到临时位置。我正在检查的一些目录是源代码目录,它们有 .svn 文件夹。我们使用 Subversion 来管理我们的代码。

搜索完所有文件后,我想删除临时缓存。听起来很容易,对吧?

由于某种原因,我的所有 .svn 目录都不会从缓存中删除。他们使应用程序崩溃。

由于某些原因(太深,无法在此讨论),我必须使用临时文件夹,因此出于政治原因,仅“扫描原始文件”是不可能的。

我可以进入资源管理器并删除它们。没问题。没有警告。只是删除。但代码崩溃并显示“访问 {file} 被拒绝”。我对这个一无所知,所以任何帮助将不胜感激。

虽然为了您的理智起见,我稍微简化了该函数,但代码确实如此简单。

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);
}

* 更新*:异常为 UnauthorizedAccessException。文本为“拒绝访问路径 'C:\temp\cache\some-sub-dirs\.svn\entries'”。

它发生在 XP、XP-Pro 和 Windows 7 下。

* Update 2 * 我的验证甚至没有尝试查看 subversion 文件。不过,我确实需要它们。这是政治废话的一部分。我必须证明每个文件都被复制了......无论它是否被扫描。

我意识到 File.Delete 的常见嫌疑是什么。我明白 UnauthorizedAccessException 的含义。我没有访问权限。这是理所当然的。但我只是复制了文件。我如何不能访问该文件?

* 更新 3 * 答案就在“只读”标志中。这是我用来修复它的代码:

    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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

云朵有点甜 2024-08-23 20:34:09

不明白你想做什么,但是将其 chmod 为 777 或 775 怎么样。:-/

编辑:

注意到你在 Windows 上。您必须更改权限。不知道 Windows 是如何做到这一点的:-/

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 :-/

↘人皮目录ツ 2024-08-23 20:34:08

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

删除文件之前尝试重置只读属性。我真的不懂任何 C#,但快速谷歌表明这可能会解决问题:

File.SetAttributes(file, FileAttributes.Normal);

As far as I recall, Subversion marks the files in its .svn subdirectories as read-only.

Try resetting the read-only attribute before deleting the file. I don't really know any C#, but a quick Google suggests this might do the trick:

File.SetAttributes(file, FileAttributes.Normal);
拔了角的鹿 2024-08-23 20:34:08

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

// ...对缓存执行一些操作以验证我需要的内容。

如果您打开文件并忘记关闭它,您仍然可以对其进行独占访问,因此以后无法删除它。

The only problem I see would be in this part:

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

If you do open the file and forget to close it, you still have exclusive access to it, and thus can't delete it later on.

是伱的 2024-08-23 20:34:08

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

system.io.file.delete

上面的链接显示,在以下情况下您会收到 UnauthorizedAccessException

调用者没有所需的权限。

-或-

路径是一个目录。

-或-

路径指定了一个只读文件。

这是其中之一。

Sounds like you don't have access to delete the file...

system.io.file.delete

The above link says you get UnauthorizedAccessException when:

The caller does not have the required permission.

-or-

path is a directory.

-or-

path specified a read-only file.

It's one of those.

不必了 2024-08-23 20:34:08

听起来像是权限问题。不过,这很棘手,因为如果 File.Copy 已经可以工作,你显然拥有写访问权限......

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

Sounds like a permissions issue. Tricky one though as you obviously have write access if the File.Copy already works....

Only thing I could think of is the file still has a handle opened somewhere (as others have suggested perhaps in your do some stuff to the cache part).

黑凤梨 2024-08-23 20:34:08

首先:“崩溃”意味着异常,对吗?哪一个?你能抓住它并展示它吗?

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

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

First of all: "Crash" means an exception, right? Which one? Can you catch it and show it?

Second thing: You are copying subversion repositories, although you don't care about the subversion metadata? That's what svn export is about (no .svn directory in the target).

The answer to the first question is what you really need to provide. Maybe something grabs the .svn and locks some files. TortoiseSVN maybe (to give you nice overlay icons..)?

就像说晚安 2024-08-23 20:34:08

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

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

稍微更改一下以暂时不触发 cmd 窗口,这是可用的互联网上到处都是)

If a folder contains read only files, Directory.Delete won't delete it and raise the exception you're getting. For future visitors of this page, I've found a simple solution which doesn't require us to recurse through all the files and changing their read-only attribute:

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

(Change a bit to not to fire a cmd window momentarily, which is available all over the internet)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文