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...
Related
I want to create a simple program that can access all the files in the folder, all of which being the same type of file, and then use a for each to rename all of them.
The new name will be IMG### with the ### being incremental. I know how to do this. How do I reference all the images to be used in a for each?
Example:
For Each X in Folder {
rename stuff here for x
}
All I need to know is how to reference file in the spot of x.
You can go about it along this line of thought:
var files = Directory.GetFiles(path, "*.", SearchOption.AllDirectories);
foreach (string item in files)
{
//You can rename by moving the files into their 'new names'.
//The names are full paths
File.Move(oldName, newName);
}
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);
I am currently writing a program which searches My Documents. Currently my program is able to search and copy the main my documents folder but I am unable to make it search sub directory's within the main my documents directory. I have tried multiple methods but none seem to be working out.
Currently I am using the below code to dump the files location into an array called files. sourcePath is declared in an array before hand.
string[] files = System.IO.Directory.GetFiles(sourcePath[loopcounter]);
I then have a loop which copy's the files over to another directory
foreach (string s in files)
Any help as to how to fill the array files with details of files in the sub directories of a folder would be very handy. Thanks in advance!
Use research by pattern and specify you want use recursion :
var allFiles = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
"*",
SearchOption.AllDirectories);
foreach (var item in allFiles)
{
// Do Stuff...
}
If you want details about each file, then GetFiles returns you array of names. Pass each name to FileInfo API.
i'm using 'folderBrowserDialog1.SelectedPath' to have a path to chosen folder. How can i see the names of another folders in folder which is selected?
DirectoryInfo dir = new DirectoryInfo(folderBrowserDialog1.SelectedPath);
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
Console.Out.WriteLine(subDir.Name);
}
You're looking for the Directory.GetDirectories method.
The FolderBrowserDialog component is displayed at run time using the ShowDialog method. Set the RootFolder property to determine the top-most folder and any subfolders that will appear within the tree view of the dialog box. Once the dialog box has been shown, you can use the SelectedPath property to get the path of the folder that was selected
You probably want to use the DirectoryInfo.GetDirectories method. See here: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.getdirectories(v=vs.71).aspx
The System.IO namespace contains a lot of tools for this. The Directory.GetDirectories you can use to get al subfolders.
For example:
var folder = folderBrowserDialog1.SelectedPath;
var subFolders = System.IO.Directory.GetDirectories(folder);
I have application which creates some windows user accounts, on uninstallation I remove the Windows User Account, but the folder for that user remains there (For example C:\Documents and Settings\UserName\"
How can I remove that folder using C#?
Thanks,
Something along the lines of this?
DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
dir = dir.Parent.Parent.Parent;
DirectoryInfo[] userDirs = dir.GetDirectories(userName);
foreach (DirectoryInfo userDir in userDirs)
{
userDir.Delete(true);
}