问题描述
我要提取一个ISO,然后从提取的ISO中复制一个文件夹.问题在于提取的文件是只读的.我试过从属性菜单和c#中的代码更改它.都不起作用.
I'm extracting an ISO and then copying a folder from the extracted ISO. The problem is that the extracted files are read-only. I've tried changing it from the properties menu, and from code in c#. Neither worked.
我用于提取ISO的代码在另一个问题中:
The code I used for extracting the ISO is in another question: extract ISO with winrar automatically with c# or batch
我正在寻找一种方法来更改提取的ISO的属性,以便可以从其子文件夹进行复制,或者仅更改子文件夹的权限.
I'm looking for a way to either change the attributes of the extracted ISO so that I can copy from its subfolders, or to just change the sub folders permissons.
预先感谢
更新
新代码
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中的文件不是只读的,只有文件夹本身是
The files in subTest are not read only, only the folder itself is.
目标路径转到C:\ users \ mydocs \ ISODump \ subTest
Destination path goes to C:\users\mydocs\ISODump\subTest
引发access denied
异常后,我仍然可以手动复制文件夹
After access denied
exception is thrown I can still copy the folder manually
更新2
解决方法
出于我的目的,找到了解决方法. directory.move
通过移动文件夹而不是复制文件夹来达到我想要的目的.
Found a work around, for my purposes. directory.move
achieves the purpose I wanted by moving the folder, instead of copying it.
谢谢
推荐答案
您可以尝试更改属性:
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;
}
您可以尝试将此属性以递归方式设置到所有子目录和文件:
You can try this to recursively set the attribute to all sub directories and files :
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);
}
}
测试用户是否有权访问文件夹-检查,如果仍然存在问题,那么我认为解决方案应该使用 Windows Shell API
这篇关于提取的文件始终是只读的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!