I found this piece of code to search a for a file that containing a pattern (extension, part of the name)
void DirSearch(string dir, string pattern)
{
try
{
foreach (string d in Directory.GetDirectories(dir))
{
foreach (string f in Directory.GetFiles(d, pattern))
{
Console.WriteLine("File found: {0}", f);
}
DirSearch(d);
}
}
catch (Exception ex)
{
Console.WriteLine("Somethis unexpected happened: ", ex);
}
}
I've always thought that the recursive algorithms are an easy small and elegant way to solve a problem, I first learned that in college while I was studying some of the "classical" samples of recursive problems, like fractals, the problem of the 8 queens, tree search, and other graph related problems, the-horse-in-the-chess-board (I am not sure of the name), and others
I'll try to remember some of those problems and try to solve them again using C#
I found a way to get the print jobs from the spoiler using Management objects and WMI
public static void GetPrintJobs()
{
string searchQuery = "SELECT * FROM Win32_PrintJob";
ManagementObjectSearcher searchPrintJobs = new ManagementObjectSearcher(searchQuery);
ManagementObjectCollection prntJobCollection = searchPrintJobs.Get();
foreach (ManagementObject prntJob in prntJobCollection)
{
try
{
string document = prntJob.Properties["Document"].Value.ToString();
string color = prntJob.Properties["Color"].Value.ToString();
string host = prntJob.Properties["HostPrintQueue"].Value.ToString();
string owner = prntJob.Properties["Owner"].Value.ToString();
int id = int.Parse(prntJob.Properties["JobId"].Value.ToString());
int pages = int.Parse(prntJob.Properties["TotalPages"].Value.ToString());
Console.WriteLine("{0} Document '{1}', pages {2} {3}, sent by {4}\\{5}", id, document, pages, color, host, owner);
}
catch (Exception ex)
{
Console.WriteLine("Exception getting print jobs: " + ex);
}
}
}
one note about this code, it'll only work in Vista/LHS, to make it work in win xp/2k3 just don't ask for the property Color which aparently does not exist in previous versions of Windows.
you can find more information about the properties of the Win32_PrintJob class here http://msdn2.microsoft.com/en-us/library/aa394370.aspx
and a little more about WMI http://msdn2.microsoft.com/en-us/library/aa394582.aspx which in my opinion is a great way to manage windows, but it is kind of slow
For purposes of ilustrate this method:
PRINCIPAL will be the PRINCIPAL database server
MIRROR will be the MIRROR database server
myDB will be the database
MIRRORBackup the folder created in PRINCIPAL to store the backups
EP5022 will be the endpoint name and I will use the port 5022 to configure it
I will create an anonymous login and grant it access to connect using the endpoint, this is not very secure, so you may wan to change it
No witness is configured, if you need one, you’ll have to configure it as well
For each Database do:
Set Recovery mode, execute in PRINCIPAL:
ALTER DATABASE MyDB SET RECOVERY FULL
Create Backup Folder in PRINCIPAL(i.e. c:\backup)
Share the Backup Folder (make sure other users can modify the folder)
Create Endpoints:
Execute in PRINCIPAL
if Not Exists (SELECT name, role_desc, state_desc FROM sys.database_MIRRORing_endpoints where name = 'EP5022')
Begin
CREATE ENDPOINT EP5022
STATE=STARTED
AS TCP ( LISTENER_PORT = 5022 ) FOR DATABASE_MIRRORING(ROLE = PARTNER)
End
if not exists( select * from sys.syslogins where loginname = 'NT AUTHORITY\ANONYMOUS LOGON')
Begin
Create Login [NT AUTHORITY\ANONYMOUS LOGON] from windows
End
GRANT CONNECT ON ENDPOINT::EP5022 TO [NT AUTHORITY\ANONYMOUS LOGON]
Execute in MIRROR
if Not Exists (SELECT name, role_desc, state_desc FROM sys.database_MIRRORing_endpoints where name = 'EP5022')
Begin
CREATE ENDPOINT EP5022
STATE=STARTED
AS TCP ( LISTENER_PORT = 5022 ) FOR DATABASE_MIRRORING(ROLE = ALL)
End
if not exists( select * from sys.syslogins where loginname = 'NT AUTHORITY\ANONYMOUS LOGON')
Begin
Create Login [NT AUTHORITY\ANONYMOUS LOGON] from windows
End
GRANT CONNECT ON ENDPOINT::EP5022 TO [NT AUTHORITY\ANONYMOUS LOGON]
Backup Database, Execute in PRINCIPAL
BACKUP DATABASE MyDB TO DISK = '\\PRINCIPAL\MIRRORBackup\MyDB.bak' WITH FORMAT
Kill Connections, Execute in PRINCIPAL
declare @sql nvarchar(1000)
declare @spid as int
declare processcursor cursor fast_forward for
select SPID
from SYS.SYSPROCESSES p inner join sys.sysdatabases d on p.dbid= d.dbid and d.name = 'MyDB'
where SPID > 50
AND spid <> @@spid
open processcursor
fetch next from processcursor into @spid
while @@fetch_status = 0
begin
set @sql = ' kill ' + convert(varchar, @spid)
exec (@sql)
fetch next from processcursor into @spid
end
close processcursor
deallocate processcursor
Restore Database, Execute in MIRROR
if not exists(select * from sys.databases where name='MyDB')
Begin
Create Database MyDB
End
RESTORE DATABASE MyDB FROM DISK = '\\PRINCIPAL\MIRRORBackup\MyDB.bak' WITH REPLACE, NORECOVERY'
Backup Log, Execute in PRINCIPAL
BACKUP Log MyDB TO DISK = '\\PRINCIPAL\MIRRORBackup\MyDB_log.bak'
Kill Connections, Execute in MIRROR
declare @sql nvarchar(1000)
declare @spid as int
declare processcursor cursor fast_forward for
select SPID
from SYS.SYSPROCESSES p inner join sys.sysdatabases d on p.dbid= d.dbid and d.name = 'MyDB'
where SPID > 50
AND spid <> @@spid
open processcursor
fetch next from processcursor into @spid
while @@fetch_status = 0
begin
set @sql = ' kill ' + convert(varchar, @spid)
exec (@sql)
fetch next from processcursor into @spid
end
close processcursor
deallocate processcursor
Restore Log, Execute in MIRROR,
RESTORE Log MyDB FROM DISK = '\\PRINCIPAL\MIRRORBackup\MyDB_log.bak' WITH NORECOVERY'
Enable MIRRORing:
Execute in PRINCIPAL
ALTER DATABASE MyDB SET PARTNER = 'TCP://PRINCIPAL.mydomain.com:5022'
Execute in MIRROR
ALTER DATABASE MyDB SET PARTNER = 'TCP://MIRROR.mydomain.com:5022'
Now everything should be ok now, and the Database is already configured.