I am working on an application that requires that some files are copied to a different folder. I use the following:
DirectoryInfo dir = new DirectoryInfo(path);
foreach (FileInfo filesindires in dir.GetFiles())
{
FileSecurity ds = filesindires.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule("Authenticated Users",
FileSystemRights.FullControl, AccessControlType.Deny));
filesindires.SetAccessControl(ds);
}
With that method I deny the user from opening the file, but I would like to only prevent copying. How can I prevent that a file is copied while allowing the user to read it?
If you can read it, you can copy it.
Related
In my C# forms application, I try to download the data in a directory of my SFTP Server. The data should be stored in a folder which I want to create in "MyDocuments". When the folder is created, I receive an Renci error "failure" because the folder is "read-only".
I tried many ways to create a folder, but I in most ways I used I either got an error, that I don't have the permission to create a folder, or I got an empty file instead of a folder. Right now I got a folder, but unluckily it is read only.
String localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\MyNewFolder\\";
if (Directory.Exists(localPath))
{
Console.WriteLine("Folder already exists");
}
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
DirectoryInfo directory = new DirectoryInfo(localPath);
DirectorySecurity security = directory.GetAccessControl();
}
I expect the folder not to be read only, so that I can safe data in it using my programm. Anyone knows why my code still creates a read only one?
I believe you have to set the following using the DirectorySecurity object:
DirectorySecurity securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule(#"Domain\Account", FileSystemRights.FullControl, AccessControlType.Allow));
Then you can create the directory using the following:
DirectoryInfo di = Directory.CreateDirectory(#"directoryToCreatePath", securityRules);
EDITED:
Once you've created the directory using Directory.CreateDirectory(), you can then apply the following to the folder. This will allow the user you've specified to have FullControl of the folder. You can check the permissions for that user via Properties > Security
DirectoryInfo directory = new DirectoryInfo("C:\\CreatedFolder");
DirectorySecurity security = directory.GetAccessControl();
security.AddAccessRule(new FileSystemAccessRule(#"USERNAME",
FileSystemRights.FullControl,
AccessControlType.Allow));
directory.SetAccessControl(security);
The files I want to zip are in different folders
The folder structure is as follows:
FileId/FileType/File.extension
FileId/FileType/File.extension
I'm trying to create a zip with the following structure
FileType/File.extension
FileType/File.extension
I have tried a stackoverflow link but couldn't get it to work in my scenario.
I have the files in the zip directly. But I couldn't find a good source to have the files in a specific folder.
Code I have referred from previous posts:
using (ZipArchive archive = ZipFile.Open(zipPath + #"\release.zip", ZipArchiveMode.Update))
{
ZipArchiveEntry readmeEntry;
DirectoryInfo d = new DirectoryInfo(folderToAdd);
FileInfo[] Files = d.GetFiles("*");
foreach (FileInfo file in Files)
{
archive.CreateEntry(preset.FileFormat);
readmeEntry = archive.CreateEntryFromFile(newFile, fileName);
}
}
I also tried creating a zip file in update mode and then as follows:
ZipFile.Open(zipPath + #"\release.zip", ZipArchiveMode.Update);
ZipFile.CreateFromDirectory(folderToAdd, zipPath + #"\release.zip", CompressionLevel.Fastest, true);
But the CreateFromDir breaks with an exception saying that the release.zip already exists.
Should I copy my files into respective folders and then apply the CreateFromDir method, or there is a better way to do this without re-copying and then CreateFromDir on top.
Delete file using C# from directory is holding the process for long time. How to kill process once the file is deleted.
Tried with 2 options of deleting the file,
Option 1:
path = #"C:\temp\a.xml";
File.Delete(path);
Option 2:
path = #"C:\temp\";
DirectoryInfo CVfiles = new DirectoryInfo(path);
foreach (FileInfo CVfile in CVfiles.GetFiles())
{
CVfile.Delete();
}
Update
(from clarification in comments)
path = #"C:\temp\a.xml";
DirectoryInfo CVfiles = new DirectoryInfo(path);
foreach (FileInfo CVfile in CVfiles.GetFiles())
{
CVfile.Delete();
}
Thanks,
You could try
C# delete a folder and all files and folders within that folder
Ultimately the deletion will take as much as deletions needs.
How to kill process once the file is deleted.
You can just exit the program.
I'm trying to copy files from external hard disk to folder in my desktop, in order to do that I have to take ownership of the folders and the files in the external hard disk, I read in previous questions about it how it has to be and my code looks like this:
using (new ProcessPrivileges.PrivilegeEnabler(Process.GetCurrentProcess(), Privilege.TakeOwnership))
{
directoryInfo = new DirectoryInfo(path);
directorySecurity = directoryInfo.GetAccessControl();
directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);
Directory.SetAccessControl(path, directorySecurity);
}
When I run this code I get an exception in the line:
directorySecurity = directoryInfo.GetAccessControl();
The exception is:
"unauthorizedAccessException was caught" "Attempted to perform an
unauthorized operation".
Why is that happen? And how can I copy these folders and files?
Using Visual Studio 2010 C#. I'm attempting to delete a folder in C:/Windows/MyFolderA where MyFolderA is a folder placed there by my software - Not Microsoft's.
I've used this code to delete the contents of the folder and the folder itself:
foreach (FileInfo tempfi in listOfMSIInstallers)
{
//Delete all Files
DirectoryInfo localDirectoryInfo = new DirectoryInfo(targetDirectory);
FileInfo[] listOfMSIInstallers = localDirectoryInfo.GetFiles("*",SearchOption.AllDirectories);
File.SetAttributes(tempfi.FullName, File.GetAttributes(tempfi.FullName) & ~FileAttributes.ReadOnly); //Remove Read-Only
File.Delete(tempfi.FullName); //Delete File
string parentFolderPath = "C:/Windows/MyFolderA"; //Example string for StackOverflow
//Remove ReadOnly attribute and delete folder
var di = new DirectoryInfo(parentFolderPath);
di.Attributes &= ~FileAttributes.ReadOnly;
Directory.Delete(parentFolderPath);
}
If I attempt to delete the folder I get an exception
"System.IO.IOException: The directory is not empty".
Showing invisible files on my GUI I do not see any. Looking at the folder with a command prompt there appears to be 2 directories: 1 named . and the second named .. (not too familiar with command prompt dir so I don't know if they're temp names or if those are the actual directory names) both at 0 files and 0 bytes.
Debugging through looking at the FileInfo[] object, it doesn't grab the invisible files found from command prompt.
Any ideas how I can delete the files/directory?
Try
Directory.Delete(parentFolderPath, true);