I have a problem very curiosity.
I am delete files into folders this is the algoritm
System.IO.DirectoryInfo di = new DirectoryInfo(folderPath);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
I choose the directory and the algorithm deletes: folders, subfolders and everything related without problem.
The problem occurs when I download a compressed file, which has folders and subfolders (it is important to mention, that the data in the compressed file is what I should actually delete).
and the software reports an error: Access denied
The truth is that I do not understand, the files of that compressed (.zip) specifically some folders of that compressed file are the ones that will not let me delete.
Is there something missing in the algorithm that allows deletion?
At first, to recursively delete folder with all files and subfolders you can just call Directory.Delete(folderPath, true).
As described in documentation, you can have UnauthorizedAccessException if file or folder is protected. One of solutions you can do is to require your application to launch with admin privilegies as described in answer https://stackoverflow.com/a/2818776/10115818
Related
NOTE: To be clear, I do not want to empty the recycle bin as answered in other questions.
Using answers from other questions on both Stack Overflow and Super User I have discovered the way to get the location of one's recycle bin in C# is:
#"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString()
However, when I run the following code I am getting different files to what is actually in my recycle bin:
string location = #"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
foreach (string file in Directory.GetFiles(location))
{
Console.Writeline(file);
}
How can I correctly get all the files in my recycling bin? My needs are to access when the files were last used and to possibly then restore them.
Thanks.
It's not as straight forward as initially thought because the Recycle Bin doesn't have a physical location, it's a virtual folder. To get a list of all files in the Recycle Bin and make this work you need to add a reference to shell32.dll (located in %SystemRoot%\system32\shell32.dll).
public static void Main(string[] args)
{
Shell shell = new Shell();
Folder folder = shell.NameSpace(0x000a);
foreach (FolderItem2 item in folder.Items())
Console.WriteLine("FileName:{0}", item.Name);
Marshal.FinalReleaseComObject(shell);
Console.ReadLine();
}
REF: http://www.dreamincode.net/forums/topic/161500-play-with-recycle-bin-in-code/
To get the LastModified Property of the files you can see my answer here:
https://stackoverflow.com/a/11660616/
Also note there's a Recycle Bin for each HDD so you will have to check all drives.
To restore files here is the solution in C#: https://stackoverflow.com/a/6025331/495455
So I've been working on a new programm right now and I'm stuck at one function and I don't know how to solve this problem.
So I'm trying to make a button which searches and prints paths of files and folders which have a specific name. After I tried some functions and they all failed, I found this and it should work but it just doesn't print anything.
string[] files = Directory.GetFiles(#"C:\Program Files", "NameOfFileOrFolder",
SearchOption.AllDirectories);
foreach (var file in files)
{
Console.WriteLine(file);
}
Btw. the console does not even open itself...
You can try to read this: https://msdn.microsoft.com/en-us/library/ms143316(v=vs.110).aspx
Also you need administrator's privilege for using current path.
Is there a way to ONLY zip files and directories at the same time. I was using ZipArchive but is not working for Directories. The directory that I want to add in the Zip file has more directories and a zip file inside, so it's a nested directory. Can someone please assist.
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
foreach (FileInfo file in filesToZip)
{
zipArchive.CreateEntryFromFile(Path.Combine(filePath, file.Name), file.Name);
}
}
}
Have you tried ZipFile.CreateFromDirectory?
ZipFile.CreateFromDirectory(filePath, zipFilePath);
If you need to zip up an entire directory (including sub-folders), this may be simpler.
To create an empty file or directory, use CreateEntry(String)
Include the slash for directories
zipArchive.CreateEntry(#"DirectoryName\");
I just wanted to see if anyone else got this issue.
I am trying to copy files from one directory to another, however when it gets to a file copy such as this:
//copy each file
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
It gives me an error such as "DirectoryNotFound" Exception. Even though the directory exists.
Has anyone run into this problem and what would a good solution around it be?
Thanks
I need to combine 3 files into 1 zip file and make it available to download for the user. I am able to achieve my requirement except one thing: it zips the files into the subfolders.
For example, my files are located like the following:
C:\TTCG\WebSites\Health\ABC.CSV
C:\TTCG\WebSites\Health\XYZ.CSV
C:\TTCG\WebSites\Health\123.CSV
But in the zip file, it zip the files in the folder by using "TTCG\WebSites\Health\" as the path. Please see the attach file.
I don't want the folders in the path. I just want 3 files in the zip file without folders. How can I achieve that?
My codes to generate the zip file is as below:
ZipFile z = ZipFile.Create(Server.MapPath("~" + #"\Accident.zip"));
//initialize the file so that it can accept updates
z.BeginUpdate();
//add the file to the zip file
z.Add(Server.MapPath("~" + #"\ABC.csv"));
z.Add(Server.MapPath("~" + #"\XYZ.csv"));
z.Add(Server.MapPath("~" + #"\123.csv"));
//commit the update once we are done
z.CommitUpdate();
//close the file
z.Close();
Based on the FAQ, you have to strip the folder path out manually:
How can I create a Zip file without folders?
Remove the path portion of the filename used to create a ZipEntry
before it is added to a ZipOutputStream
ZipEntry entry = new ZipEntry(Path.GetFileName(fullPath));
The FAQ can be found here.
It seems to be a limitation of the library. Hope this helps!
If you have your files in a FileSystemInfo, you can use: z.Add(file.FullName, Path.GetFileName(file.FullName));
This will add your files in the root directory of your zip.
z.Add(pathToFile, pathInZipFile);