Unable to Delete Folder Containing Invisible Files - c#

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);

Related

Why do I get message "Access to the path 'bootmgr' is denied " when trying to clear the contents of a directory

Using Windows 10 Home (20H2) on HP Spectre
I am trying to use the following code to clear the contents of a directory
public int clearDirectory(string path)
{
DirectoryInfo targetDir = new DirectoryInfo(path);
foreach (FileInfo file in targetDir.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in targetDir.GetDirectories())
{
dir.Delete(true);
}
return 0;
}
The target directory is on a USB SanDisk which has in its root dir one directory (which has a number of subdirectories) which I created and the following SanDisk files
SanDiskMemoryZone_AppInstaller.apk
SanDiskMemoryZone_QuickStartGuide.pdf
I replaced the path to the USB drive with a path to a directory on my C drive and that worked fine.
How does the bootmgr get involved in this?
In Windows you can declare for each drive if it is bootable or not.
The bootable partition in Windows is a hidden drive called system partition (and that‘s also the reason why no error was shown when you tried to delete the C:-drive)
Your SAN-drive seems to be using a file system which has a hidden folder with ACL only for the system account.
That‘s why you get this error. You have no access right to delete the file/folder.
One solution:
change your code s.t. It only selects files without a leading dot, like with regex: ^\.
If this still doesn’t fix your error then you should try use an elevated process (with adminr rights) to tackle this.
REMARK: before you delete everything from a usb stick, it would be easier to just reformat it then deleting every file.
Maybe look into Diskpart for this.

C# file delete and take long time to release process

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.

C# to copy files from one folder to another without duplicates

Im trying to copy files from "source" folder to my "destination" folder, without duplicates. I cant use destination folder to compare as it will eventually get deleted from there. I had gotten help here to make a batch file
Robocopy "source" "destination" "*chr.txt*" "*hdr.txt*" /M
that uses attribute to keep track of files copy before. However I need to do this in C# instead. I know theres command to copy
System.IO.File.Copy(sourceFile, destFile, true);
but not sure how to go about being specific on file name "*chr.txt" and taking care of duplicates.
I need to do this in C# instead
Process.Start("robocopy", "source destination chr.txt hdr.txt /M");
Here is a method I created a while ago that has worked well for me (aside from the checking for duplicates part that I just added and haven't tested). Let me know if any parts of it do not work for what you need.
private static void CopyDirectory(string from, string to)
{
var toFileNames = new DirectoryInfo(to)
.GetFiles()
.Select(f => f.Name)
.ToList();
var directory = new DirectoryInfo(from);
var files = directory.GetFiles();
foreach (var file in files)
if (!toFileNames.Contains(file.Name))
file.CopyTo(Path.Combine(to, file.Name));
var subDirectories = directory.GetDirectories();
foreach (var subDirectory in subDirectories)
{
var newDirectory = Directory.CreateDirectory(Path.Combine(to, subDirectory.Name));
CopyDirectory(subDirectory.FullName, newDirectory.FullName);
}
}

How to know a new folder is created in a particular folder

I am developing a desktop application in C#.
I have programmatically created a folder(say ABC) inside the Main folder in C drive in Windows.
I want to know whether an user has created any new folder(by simply right clicking and creating new folder) inside ABC.
If the user has created a new folder then I need to get the details of that folder like folder name and privacy too.
Thanks in advance!
You can get the subdirectories of a folder (in your example, the folder "ABC") as an array of strings by calling the method GetDirectories:
string[] subdirs = Directory.GetDirectories(#"C:\ABC");
Then, if you'd like, you can iterate through all of them:
foreach (string dir in subdirs)
//dir is a path to a subdirectory
Don't forget the using statement!
using System.IO;
You can use DirectoryInfo to get the list of subfolder
DirectoryInfo dirInfo = new DirectoryInfo(#"c:\ABC");
DirectoryInfo[] subFolders = dirInfo.GetDirectories();
I'm not sure what you mean by privacy...

Copying all the checkout files

I am using Team foundation system and I have requirement in which I want to copy all the checkout files to a local folder along with the same folder structure using C#. How can I do that?
I don't know what you mean by the "checkout files", but if you want to copy a directory you have to:
Recursively enumerate all of the files and folders in the top-level directory.
For each item that you enumerate, either create the folder in the destination directory, or copy the source file to the destination directory hierarchy.
The following will enumerate all of the files and folders in a directory:
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
Console.WriteLine("Directory {0}", dir.FullName);
// list the files
foreach (FileInfo f in dir.GetFiles(searchPattern))
{
Console.WriteLine("File {0}", f.FullName);
}
// process each directory
foreach (DirectoryInfo d in dir.GetDirectories())
{
FullDirList(d, searchPattern);
}
}
If you call that with FullDirList("C:\MyProject\", *.*), it will enumerate all of the files.
To create destination folders or copy files, change the calls to Console.WriteLine so that they do the appropriate thing. All you should have to change in the destination file or folder names is the root folder name (that is, if you're copying from C:\MyProject\ to C:\MyProjectCopy\, then the destination files are just f.FullName with the C:\MyProject\ replaced by C:\MyProjectCopy).

Categories