【问题标题】:Extracted Files are always read-only提取的文件始终是只读的
【发布时间】:2012-05-23 07:59:16
【问题描述】:

我正在提取一个 ISO,然后从提取的 ISO 中复制一个文件夹。问题是提取的文件是只读的。我尝试从属性菜单和 c# 中的代码更改它。都没有用。

我用于提取 ISO 的代码在另一个问题中:extract ISO with winrar automatically with c# or batch

我正在寻找一种方法来更改提取的 ISO 的属性以便我可以从其子文件夹中复制,或者只更改子文件夹的权限。

提前致谢

更新

新代码

 string[] folderToName = txtCopyFrom.Text.Split('\\');
        string isoName = folderToName[folderToName.Length - 1];
        isoName = isoName.Remove(isoName.Length - 4, 4);

        string copyTestFrom = txtCopyTo.Text + @"\"+ isoName + @"\Test\subTest";
        string[] folderName = txtCopyFrom.Text.Split('\\');
        string folderTestTo = folderName[folderName.Length - 1];
        folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4);
        string copyTest = txtCopyTo.Text;
        System.IO.Directory.CreateDirectory(copyTest);

        DirectoryInfo di = new DirectoryInfo(copyTestFrom);
        di.Attributes &= ~FileAttributes.ReadOnly;

        foreach (FileInfo fi in di.GetFiles())
        {
            fi.IsReadOnly = false;

            string destinationPath = Path.Combine(copyTest, Path.GetFileName(copyTestFrom));
            File.Copy(copyTestFrom, destinationPath);
        }
        MessageBox.Show("Files Copied");   

subTest 中的文件不是只读的,只有文件夹本身是只读的。

目标路径转到 C:\users\mydocs\ISODump\subTest

access denied 抛出异常后,我仍然可以手动复制文件夹

更新 2

解决方法

为我的目的找到了解决方法。 directory.move通过移动文件夹而不是复制它来达到我想要的目的。

谢谢

【问题讨论】:

  • I've tried changing it from the properties menu, and from code in c#. Neither worked.。如果您不能从属性菜单中更改它,您就不能期望它在 C# 代码中工作。权限问题?您没有尝试修改只读驱动器上的文件(例如,挂载的 iso)?
  • @KooKiz 我认为这不是权限问题,因为在引发异常后,如果我进入子文件夹并更改其中每个文件的权限,我可以做到这一点并且然后复制它们。但我想做的是在提取后直接做这一切。
  • 所以现在我们知道抛出了“拒绝访问”异常。请事先提供所有信息。究竟是哪一行代码触发了异常?
  • 嘿,我认为您的文件事先是只读的,因为我创建了一个简短的 iso 并检查了提取后没有文件是只读的...所以这不是 ISO 提取器代码的错...所以你应该使用其他一些代码[也许来自答案]来设置文件可写!
  • 对不起,我没有把它包括在内,只是我忘记了。 Access to the path 'C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst' is denied. 是抛出的异常。如果我走出 IDE 并右键单击、复制然后粘贴到我的文档中,它就可以工作,所以我确实可以访问。尝试了史蒂夫的答案,它没有用,现在正在运行 Niranjan Kala 的答案

标签: c# permissions readonly file-attributes


【解决方案1】:

尝试使用此代码

   fileInfo.IsReadOnly = False

而不是

fileInfo.Attributes = FileAttributes.Normal

上面的代码有问题: 这些行,给定你的一个 cmets 中的路径

"C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst"
                                                                           ^^^^^^^^
string folderTestTo = folderName[folderName.Length - 1];            
folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4);            
string copyTestTo = folderTestTo;    

将在 copyTestTo 中返回此值:

subt

那么你做

string destinatationPath = Path.Combine(copyTestTo, 
                                        Path.GetFileName(copyTestFrom));

这将返回像这样的路径不可能的东西

 "subt\C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst"

我认为你应该仔细检查代码中的这些段落,在那里设置一些断点并检查你的变量的值

【讨论】:

  • 我的错。 IsReadOnly=假不真
  • 您能否在开始处理之前告诉我txtCopyFrom.TexttxtCopyTo.Text 的值。这两个值是一切的开始
  • txtCopyto.Text "C:\\Documents and Settings\\user\\My Documents\\ISO DUMP!!\\extracted iso" 和 txtCopyFrom 只是服务器位置,我用它来获取 iso 名称并删除 .iso 扩展名来命名文件夹
【解决方案2】:

你可以试试这个来改变属性:

foreach (string fileName in System.IO.Directory.GetFiles(path))
{
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);

    fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly;
    // or
    fileInfo.IsReadOnly = true;
}

你可以试试这个递归地设置所有子目录和文件的属性:

DirectoryInfo di = new DirectoryInfo(directorypath);
public void Recurse(DirectoryInfo directory)
{
    foreach (FileInfo fi in directory.GetFiles())
    {
        fi.IsReadOnly = false; // or true
    }

    foreach (DirectoryInfo subdir in directory.GetDirectories())
    {
        Recurse(subdir);
    }
}

Test if user has write access to a folder-Check it, 如果还有问题,我认为应该使用解决方案 Windows Shell API

【讨论】:

  • 仍然得到上面 cmets 中概述的 Denied 异常。谢谢你,非常感谢
  • 还是有问题..尝试使用win32Api
  • 什么是 win32api,我从哪里得到它?
  • 检查最后一行答案和指定链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多