Detect removeable media - c#

I want to detect when a removal media is inserted or removed from the PC and I have read many threads of how to do it.
Reading other threads and pages gives essential two methods
Override WinProc method
Make SQL query
There is a FileSystemWatcher class, is there reallĂ˝ not a "DriveSystemWatcher" or something ?
Also, if I try the "SQL" approach it can not compile the program becosue it can not resolve "ManagementEventWatcher", even if I do
using System;
using System.Management;
Help would be appreciated
Regards Stefan.

In order to use classes from System.Management (so that you can query WMI) you need to add a reference to System.Management.dll to your project.
The following describes an approach using WMI when you've done this:
Detecting Eject/Insert of Removeable Media

Related

C# using object "alias"

// actual
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// looking for
using Deps.Base; // containing the above
I'm trying to "alias" these objects without additional overhead. Can't find the name for what I'm trying to do, and am wondering if I would be adding overhead with it.
Thanks
What you're planning to do is not possible due to the overlapping/crossing of types sharing the same name in the namespace unfortunately.
There is no way to bundle using statements in C#.
You'll almost never need to look at those statements though. Most IDE's will add them automatically and have procedures to clean them up automatically.
If you use Visual Studio and you don't want to see them you can collapse them.
There are extensions that automatically collapse (hide) them every time you open a cs file:
https://marketplace.visualstudio.com/items?itemName=Nhitze.SeeUsingsLater
I don't have any experience with this extension though. Personally, I'm not bothered by the using statements at the top.

Using EnumBuilder in C#

I am using EnumBuilder like described in https://stackoverflow.com/a/792332/910502 to create an enum based on values of a database and like'd to use this approach on a non-developer machine - I can deploy a dummy assembly, but have no post-build events and can't trigger the console app, because the assembly is already in use by the exe referencing it and therefore the access is denied.
One approach might be to have a third app, that functions as a starter:
First start the console app that creates the assembly, then start the application using it.
What is the recommended way for situations like this?
You didn't provide much details, but I think that you can achieve your goal by using simple bash/powershell script which will run 'enum-generator' and then 'app' which will expect this dll at specyfic directory.
Regarding dynamic enum generator. Have you considered using T4 Templates? Looks like a little bit cleaner approach.

Do using directives hurt performance or increase app size compared to full namespaces?

I read somewhere that when you add using System; to your .cs file, the compiler (or maybe something else) adds all System related classes to your .cs file and so maybe it is better to not always add using System; and instead reference your DateTime with its full namespace like this: System.DateTime, for example, if it is easy enough to do and few things reference System it in your .cs file.
Is that true and if so, can that hurt performance or increase the size of the app? If so, I realize that using System; is easier to write and is a convenience and so therefore, you must weigh convenience with performance. It also might be the case that only adding a using System; might not make much difference but when many references are added, maybe it could? Thanks!
Both using directive and full namespace generate the same IL.
It might be very tiny bit of extra work for Language compiler, but you want to sacrifice for readability.
Overview of the compile-time and runtime processes diagram is from Illustrated C# 2012 by Daniel Solis
Mention like
System.Console.WriteLine("Something");
this easy to Understanding purpose Only. But Developer Point of Each Time don't mention fully Qualify Names.
So That Time using Namespaces Using.System
If you use "using System;" you have access to all methods inside the System class.
Your class has a "link" to all of System.
If you write "using System.DateTime;" as example you only have access to the DateTime functions.
In my opinion it is better to use the specified using like "using System.DateTime".
With that you don't have links to "uneccessary" classes/methods in your .cs file.
Also the "using System" does not include all Methods that are inside of the System.
Examples for this are the IO class or the Xml Class.
If you want to use these you have to write "System.IO." before your method call or you have to write using System.IO;

Read File allocation table without using System.IO

i have a problem which is that i want to extract information about FAT(file allocation table) in C# but the restriction is i cannot use System.IO
so How i get drive types?cluster information e.t.c
One way might be to use WMI. I'm not sure which classes you'd need to use but I'd start looking at the Win32_LogicalDiskclass as described here.

List all System Modems

Is there a way in managed code to list the Modem/Telephony devices installed on the system?
If .Net does not have a way, could you point me in a direction?
WMI will contain all the information you need in the Win32_POTSModem class. In C# or .Net, you can utilize the System.Management namespace to query WMI.
Within .Net, you can use MgmtclassGen.EXE from the platform SDK to generate a class object representing the WMI class.
The command line would be like this:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\mgmtclassgen.exe Win32_POTSModem /L CS /P c:\POTSModem\Win32_POTSModem.cs
and then you can use that in your code:
using System;
using System.Collections.Generic;
using System.Management;
using ROOT.CIMV2.Win32;
public class MyClass
{
public static void Main()
{
foreach (POTSModem modem in POTSModem.GetInstances()) {
Console.WriteLine(modem.Description);
}
}
}
Output looks like this:
ThinkPad Modem - Internal Modem
Speed: 56000
You also might want to take a look at this article: CodeProject: How To: (Almost) Everything In WMI via C# - Part 3: Hardware.. The author has created a simple class wrapper around WMI objects similar to MgmtclassGen.exe, but its all done for you.
Just some thoughts for future generations.
#Christopher_G_Lewis provided very good solution.
But before using WMI we have to check that Windows Management Instrumentation (WMI, service name Winmgmt) is working (how to do it?). Of course, MS recommends don't touch this service, because it's part of system stuff, but people switch it off sometimes.
Moreover, sometimes it may be helpful to check WMI version before using it.
If you want to get modems list which are connected at the moment, you can check out this solution. It works slowly, but shows all connected modems and excludes Null modem cables.

Categories