How to close a particular windows explorer window in c#? - c#

I am wondering if I can close an explorer window which is communicating with my USB drive. I can get the removable disk and its drive letter by using
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (!drive.IsReady)
{
continue;
}
if (drive.DriveType == DriveType.Removable && isDirectoryEmpty(drive.Name) == true)
{
//do stuff
}
}
How do I do that ? any help would be appreciated.

You cannot use Process.GetProcessesByName("explorer") because there is always one explorer process in the returned array, and by killing it, you would kill the window task bar too.
You have to use a COM library as explained here:
https://stackoverflow.com/a/13464352/1280523

You can try like this:
foreach (Process p in Process.GetProcessesByName("explorer"))
{
if (p.MainWindowTitle.ToLower().Contains(#"yourSpecificWindow"))
{
p.Kill();
}
}

Related

Read boot sector CD drive on macOS

I am trying to read the boot sector of a cd drive so that I can extract its contents. Unfortunately, no matter what I do I can't seem to get it working. I have tried reading the directory as a file but get access denied. Tried reading right from /dev/disk# and got locked.
I have also tried checking and changing permissions, but nothing.
In Windows, I would use CreateFile & ReadFile from kernel32. I just am not sure what the macOS equivalent would be.
public static List<DriveInfo> GetDrives() => DriveInfo.GetDrives().Where(d => d.IsReady && d.DriveType == DriveType.CDRom).ToList();
public static ReadDrives()
{
var drives = GetDrives()
foreach(var drive in drives)
{
var root = drive.RootDirectory.FullName;
using (var fp = File.OpenRead(#"/Volumes/Flash - Copy/"))
{
//extract boot here
}
}
}
This is not the best answer, but this works for me, no sudo needed.
It is at least a solution for now.
var command = $"-c \"umount {inputDisc} && dd if={inputDisc} of={outputfile} bs=32k count=1 && mount {inputDisc}\"";
Process.Start("/bin/bash", command);

Search for ".myox" files on Computer

trying to write a small windows application for my company. the part i am stuck at the moment is trying to search the computer for ".myox" files (or say any file type). Below pasted is the code i have worked out. I am an amateur programmer trying to get started with coding. The issue am having at the moment with the code below is its skipping almost all locations on the computer with the exception coming up as "access denied". I have run the VS as admin, and i am an admin on the computer as well. Not sure what i am missing, but if someone can point me in the right direction, that would be amazing.
private void FindAllFiles()
{
int drvCount;
int drvSearchCount = 0;
DriveInfo[] allDrives = DriveInfo.GetDrives();
drvCount = allDrives.Count();
foreach (DriveInfo dr in allDrives)
{
lbAllFiles.Items.Clear();
drvSearchCount++;
//removable drives
if (!dr.IsReady)
break;
foreach (string dir in Directory.GetDirectories(dr.ToString()))
{
try
{
foreach (string files in Directory.GetFiles(dir, "*.myox"))
{
lbAllFiles.Items.Add(files);
}
}
catch (Exception Error)
{
}
}
if (drvSearchCount == drvCount)
break;
}
MessageBox.Show("Done searching your computer");
}
Thanks in Advance.
-Manu
I see few "potential" issues and will list them below.
First is that you're doing this on main ( UI ) thread which will block whole application giving you no feedback about current state. You can use Thread to get rid of this problem. Outcome from this operation will produce another issue which is accessing lbAllFiles because ( as i think ) it's part of the UI. You can easily get rid of this problem making a List<string> that can be filled during FindAllFiles operation and then assigned into lbAllFiles.Items.
Second issue is :
foreach (string files in Directory.GetFiles(dir, "*.myox"))
{
lbAllFiles.Items.Add(files);
}
Directory.GetFiles(...) will return only the files that are matching your pattern parameter so you can simply do :
var files = Directory.GetFiles(dir, "*.myox");
if ( files != null && files.Length > 0 )
lblAllFiles.Items.AddRange(files);
And finaly to get ( or check ) permission you can Demand() permissions as I've posted in the comment :
foreach (string dir in Directory.GetDirectories(dr.ToString()))
{
FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, dir);
try
{
permission.Demand();
var files = Directory.GetFiles(dir, "*.myox");
if ( files != null && files.Length > 0 )
lblAllFiles.Items.AddRange(files);
}
catch (Exception Error)
{
}
}
Let me know if that helped you. If not I'll try to update my answer with another solution.
One thing i noticed in your code, is that you're not navigating through ALL directories and sub-directories. For that, where you call the GetDirectories function, not only send the path, but use the enumerator Alldirectories:
foreach (string dir in Directory.GetDirectories(dr.ToString(),System.IO.SearchOption.AllDirectories))

Detecting if disk is flash drive

I wanna write program that detects flash drives.
But there's a problem.
Code:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
}
}
It works well, but it detects cdrom too. How to prevent it?
I have no answer why your code is not working. But if you want to detect USB-devices, you could also try it with the WMI like this:
ManagementObjectCollection drives = new ManagementObjectSearcher(
"SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType='USB'"
).Get();
Add the System.Management assembly to your project to do it like this.

Prevent application being runned from network drive

I wonder if it was possible to prevent or at least detect if my application is being used from a network drive instead of a local folder/drive.
I wish to inform my user that he has to copy the sources and run them locally because of heavy performance pitfalls.
Get the path to where your executable has been executed from, get the root, then find out if it is a network drive.
var root = Path.GetPathRoot(Assembly.GetEntryAssembly().Location)
var driveInfo = new DriveInfo(root));
if (driveInfo.DriveType == DriveType.Network) {
// Alert that this will be slow.
}
Note that you should read the question I linked (How can I get the application's path in a .NET console application?), as there is potentially a bit more to it than the code above, depending on your exact scenario.
to prevent ArgumentException in case GetPathRoot returns \\Share\myshare
DriveInfo driveInfo = null;
try
{
driveInfo = new DriveInfo(Path.GetPathRoot(Assembly.GetEntryAssembly().Location));
}
catch (Exception)
{
}
if (driveInfo == null || driveInfo.DriveType == DriveType.Network)
{
//not allowed
}

How to get list of USB in winform using c#?

i have tried this code to get the usb devices in connected to the computer.
This is the code:
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
cmbUSB.Items.Add(drive.Name);
}
}
cmbusb is a combobox.. here i am getting this :
E:/
G:/
but not getting the device name, like :
E:/Insforia
something like this,
how can i get this? is it possible to get this? pls help
For getting the DeviceName of E:/ try this.
DriveInfo driveInfo = new DriveInfo("E");
if(driveInfo.IsReady)
{
string deviceName = driveInfo.VolumeLabel;
}
I believe you are looking for VolumeLabel, try:
The label length is determined by the operating system. For example,
NTFS allows a volume label to be up to 32 characters long. Note that
null is a valid VolumeLabel.
foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.DriveType == DriveType.Removable)
{
if (drive.IsReady)
cmbUSB.Items.Add(drive.Name + "-" + drive.VolumeLabel);
//^^^^^^^^^^^^^^^^
//here
}
}

Categories

Resources