Hello all,
I am trying to make a module which will do two jobs:
1. Automatically runs when a CD is inserted into the cd rom drive.
2.copies the contents on CD to any location on hard disk.
I am usng C#.net 2003.
Anyone please help me!
Thanks
Hello all,
I am trying to make a module which will do two jobs:
1. Automatically runs when a CD is inserted into the cd rom drive.
2.copies the contents on CD to any location on hard disk.
I am usng C#.net 2003.
Anyone please help me!
Thanks
Automatically detect a CD and read CD contents
Nick Shen
Take a look at this to eject the CD programmatically:
http://www.csharpfriends.com/Forums/ShowPost.aspx PostID=37983
As for the problem where it won't run again, could be the fact that you did not close all the resources that were being used. I am unsure as I have not done this before.
Rashin
Thank you for writing to me!
the DriveInfo classs is not in System.IO namespcae.
I am usng v1.1 of .net. You please provide help using version 1.1.
I have done almost half of the job. I searched code and made a bit modification in that code and now when the cd is inserted in the cd rom drive, the programme (after running manually) detects cd insertion and reads files and copy all the folders from cd to the hard disk.
I am having 2 problems now.
ONE: when this programme end copying folders from cd to hard disk and is run again, it does not do the job again ( copy folders from cd to hard disk). Pleaese help me to do this.
TWO: I want to eject cd automatically when the folders are copied from cd to hard disk. ( THIS IS MORE IMPORTANT.)
here is my code:
using System;
using System.IO;
using System.Management;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace snip002
{
public class Win32
{
public Win32()
{
//
// TODO: Add constructor logic here
//
}
public enum DriveType : uint
{
Unknown = 0,
NoRootDir = 1,
Removable = 2,
Fixed = 3,
Remote = 4,
CdRom = 5,
RamDisk = 6
}
[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern DriveType GetDriveType(
string rootPathName);
}
class WMIEvent
{
static void Main(string[] args)
{
WMIEvent we = new WMIEvent();
ManagementEventWatcher w = null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();
// Bind to local machine
ConnectionOptions opt = new ConnectionOptions();
opt.EnablePrivileges = true; //sets required privilege
ManagementScope scope = new ManagementScope( "root\\CIMV2", opt );
try
{
q = new WqlEventQuery();
q.EventClassName = "__InstanceModificationEvent";
q.WithinInterval = new TimeSpan( 0, 0, 1 );
// DriveType - 5: CDROM
q.Condition = @"TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5";
w = new ManagementEventWatcher( scope, q );
// register async. event handler
w.EventArrived += new EventArrivedEventHandler( we.CDREventArrived );
w.Start();
// Do something usefull,block thread for testing
Console.WriteLine("Inserted....");
Console.ReadLine();
}
catch( Exception e )
{
Console.WriteLine( e.Message );
}
finally
{
w.Stop();
}
}
public static void copyDirectory(string Src,string Dst)
{
String[] Files;
if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files=Directory.GetFileSystemEntries(Src);
foreach(string Element in Files)
{
// Sub directories
if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}
// Dump all properties
public void CDREventArrived(object sender, EventArrivedEventArgs e)
{
// Get the Event object and display it
PropertyData pd = e.NewEvent.Properties["TargetInstance"];
if (pd != null)
{
ManagementBaseObject mbo = pd.Value as ManagementBaseObject;
// if CD removed VolumeName == null
if (mbo.Properties["VolumeName"].Value != null)
{
Console.WriteLine("CD has been inserted");
string[] lg;
lg=System.IO.Directory.GetLogicalDrives();
string[] drives = Environment.GetLogicalDrives();
string[] files={"0","1"};
string[] dir;
//File[] f;
//Directory[] d;
foreach(string drive in drives)
{
switch(Win32.GetDriveType(drive))
{
case Win32.DriveType.CdRom:
//Console.WriteLine("CdRom");
dir=Directory.GetDirectories(drive);
copyDirectory(drive,"F:\\backup " + DateTime.Now.ToString("d").Replace("/","-")+ "\\");
Console.WriteLine("End");
break;
case Win32.DriveType.Fixed:
//Console.WriteLine("Fixed");
break;
}
}
}
else
{
Console.WriteLine("CD has been ejected");
}
}
}
}
}
Please write back soon!
thank you indeed
Carl Prothman
http://msdn.microsoft.com/library/default.asp url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/autorun/autoplay_intro.asp
http://msdn.microsoft.com/msdnmag/issues/01/11/autoplay/
cosimog
The code given does exactly what it should do, detect if the CDROM was inserted in the drive.
what errors (if any) do you get
And what program do you want to automatically start running the program on the CD if it is on the CD then create an autorun.ini and give it the name of the application to run on the CD.
Guy F
Thanks you ahmedilyas for writing back to me!
yes, when I eject the cdrom drive and and put it back in the drive again, it does not run.
I am using Win32 to achieve my task but still can't find how to use it to eject cd automatically.
Please help me more!
I am close to finish my programme.
Thank you indeed,ahmedilyas
robbins_m
to eject the CDROM drive, I believe you have to use Win32/PInvoke to do so.
Drive classes are new in .NET 2.0 and cannot be used in .NET 1.1 as you have spotted/fixed.
When you say that the program doesnt run again after copying files from CD to HDD, what exactly do you mean
do you mean that when you eject the CDROM drive and put it back in the drive again, that it does not run
Bengab
I got this working in a C# Windows app. However, the A: drive keeps spinning.
The documentation states that one shouldn't set the WithinInternval if you provide a Event Handler for the Event. I thought I might just not set the WithinInterval by commenting it out but that raises a COM error:
Exception from HRESULT: 0x80042002
The documentation states that value of the property is:
Null, if there is no polling involved; otherwise, a valid TimeSpan value if polling is required
(EDIT: Damn thing at half the reply)
If you comment out the WithinInterval you get a underlying COM or DLL error:
Exception from HRESULT: 0x80042002
So, I guess you need it. How do I stop the A: drive from spinning due to the polling
Kirk MS
Thank you for writing to me again, ahmedilyas!
But this code provided in this link does not do the job that I need.
I want that when cd is inserted into the cd rom drive, the programme automatically start running and then start copying the files and folders on the cd to the hard disk.
I have checked this code and compiled and run , but it is not doing the job i need to do.
Please help me more as I have to finish this task today and I am stuckked now!
Thank you indeed
joedotnot
You would need to use the Management namespace and use some WMI calls/events
Take a look at this:
http://www.publicjoe.f9.co.uk/csharp/snip/snip002.html
sancai
please anyone help in getting this job done! i have no idea yet.
Mclarenvj
you could use the DriveInfo[] class and get the collection of drives on the system.
Then iterate through each one, check if it is of a CDRom, if it is, check if it is ready and finally if it is, access the drive and copy files.
DriveInfo[] theCollectionOfDrives = DriveInfo.GetDrives();
foreach (DriveInfo curDrive in theCollectionOfDrives)
{
if (curDrive.DriveType == DriveType.CDRom)
{
if (curDrive.IsReady)
{
//ToDo: Copy files from current drive
}
}
}
Orniana
Thanks you very much, ahmedilyas for your continuous help!
The code you have provided through the link is very helpful. I have checked it and it works fine. But I had done it before using some other code.
Anyway thank you once again,thank you indeed
Jose G. de Jesus Jr MCDBA
Thank you both for replying!
Ahmed, your code is very helpfull that you have written in this post.
Can you please send me the code that detects cd when it is inserted into cd rom drive.
Thanks you all
_wim