更改文件的只读属性

发布于 2024-07-13 02:16:08 字数 775 浏览 3 评论 0原文

我似乎无法更改文件上的只读标志。 我在 Vista 和 XP 上尝试过,结果相同,所以我不认为这是与 UAC 相关的问题。

但我所做的一切似乎都不起作用。 请参阅下面的示例。 有人可以告诉我我做错了什么吗?

public bool UpdateResFile(string fileName, string language, string objectName, string NewValue)
{
    FileInfo fi = new FileInfo(fileName); 
    try
    {
        //Do Stuff                
        xDoc.Save(fileToUpdate);
    }
    catch (UnauthorizedAccessException)
    {
        //fi.IsReadOnly = false;
        File.SetAttributes(fileName, FileAttributes.Normal);
        //fi.Attributes -= FileAttributes.ReadOnly;
        return UpdateResFile(fileName, language, objectName, NewValue);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
    return true;
}

I can't seem to change the read only flag on a file. I've tried this on Vista and XP with the same result, so I don't think it's a UAC related issue.

Nothing I've done seems to work though. See the sample below. Can someone tell me what I'm doing wrong?

public bool UpdateResFile(string fileName, string language, string objectName, string NewValue)
{
    FileInfo fi = new FileInfo(fileName); 
    try
    {
        //Do Stuff                
        xDoc.Save(fileToUpdate);
    }
    catch (UnauthorizedAccessException)
    {
        //fi.IsReadOnly = false;
        File.SetAttributes(fileName, FileAttributes.Normal);
        //fi.Attributes -= FileAttributes.ReadOnly;
        return UpdateResFile(fileName, language, objectName, NewValue);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        return false;
    }
    return true;
}

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

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

发布评论

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

评论(2

转身泪倾城 2024-07-20 02:16:08

无法在 UnauthorizedAccessException 中更改文件的只读属性,因为应用程序正在使用您在计算机中拥有的权限执行。

如果您无权正常将此文件更改为只读,您的应用程序也不会更改此属性。

亲切的问候。
何塞马.

Is not possible to change the read only attribute of a file at UnauthorizedAccessException cause the application is executing with the permissions that you have in the computer.

If you dont have permissions to change normally this file to read only, your application will not change this attribute either.

Kind Regards.
Josema.

等待圉鍢 2024-07-20 02:16:08

当您收到 UnauthorizedAccessException 时,您正尝试更改文件上的只读标志。 你不能那样做。 您可以(并且应该)做的就是通知用户他/她没有在那里保存文件的访问权限,并提出将文件保存在其他地方。

You are trying to change read-only flag on file WHEN you get an UnauthorisedAccessException. You can't do that. All you can (and should) do is to notify user that he/she has no access rights to save file there, and offer to save somewhere else.

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