|
SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT);
private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild,
uint dwEventThread, uint dwmsEventTime)
|
|
|
|
|
Hello gurus,
As the subject says, I'd like to know how I can detect if a service is running in C#?
Is there an equivalent of the vb script IsServiceRunning function in C#???
Can someone provide me a code snipet please?
Best regards.
Fred.
There is no spoon.
|
|
|
|
|
Hi,
ServiceController.Status give the status of the service. I guess you can use this.
Thanks and Regards
Mahe
|
|
|
|
|
using system.serviceprocess
// u have to add a dll file
System.ServiceProcess.ServiceController svcCtrl = new ServiceController("your service name ");
string mystatus=svcCtrl.status.ToString();
Console.Writeline(mystatus);
|
|
|
|
|
Hi,
It works fine now
Thanks a lot!
Best regards.
There is no spoon.
|
|
|
|
|
Hi
I would like to generate the text file with the help of Template Programming using C#. Please explain me how .Net 2003 supports and what are all the options in framework to create such kind of programming.
Please provide me a example also.
Thanks.
|
|
|
|
|
Templates are called generics in C#. I can't see any reason why you'd need templates to create a text file. Generics do not exist in .NET 2003.
The System.IO namespace contains all the classes you need to interact with the file system. This[^] is a good resource on the subject.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
I have tested this significantly and it gets curious-er. I installed a drive that has nothing but MP3 files on it. I went in a command com and issued a “Dir *.mp3 /b /s > test.txt” to create a list of all the files on the drive. I read the file with a text editor and there are 17,247 files listed. Okay I run the program I have been testing and it finds only 16800 files. Here is the code I used to which I introduced a counter with which I report to a message box in order to confirm the exact number in the array. The number in the array agrees with countFiles.
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, "*.mp3"))
{
arrText.Add(f);
countFiles++;
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
Upon examintion of the array in the list box there is also 16,800 files in the list box but I notice that I have a directory for “World” music which is not present in the list. Out of curiosity I change the name of the directory from “World” to “aWorld” and run the program without any other changes. The program reports 17,005 files which reflects the approximate number of files in the “World” directory.
It seems that there is something strange happening in DirSearch that is beyond my feeble mind. Since it will not read anything alphabeticaly beyond “Rock and Roll” could there be some kind of musical prejudice at work? That’s a joke.
|
|
|
|
|
>dir also lists the directory names themselves, whereas your code does not count them.
Luc Pattyn
|
|
|
|
|
This would not account for the difference of 200 files when "world" is renamed to "sworld". I do understand what you are saying and will set up a more precise test when I get rid of the major bugs. There is only 15 directories on the root of my test drive with no more that 15 or so sub directories.
Tnx for the suggestion
|
|
|
|
|
if I just do a "DIR *.* /s" this should yield a correct count of the files on a drive shouldn't it?
|
|
|
|
|
dir /s promises to list files
and GetFiles() returns a list of files
In .NET file seems to mean leaf node in the file system's hierarchy;
in DOS I dont know, maybe they still realized a directory was actually also a logical unit
of information, aka file.
My observations indicate a difference between both worlds, I guess that is what
progress is all about ?
Luc Pattyn
|
|
|
|
|
No files are in the root directory ?
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
None unless they are hidden system files.
|
|
|
|
|
Well, perhaps they are.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
WE have differences of many hundreds of files. I have now verified that all directories with names more than alpha 't' are not being read. For example the directory "world" is not read change name to "sworld" and it is included. Change the name to "tworld" and it is not.
|
|
|
|
|
I suggest you adapt your code to produce a file similar to the one from the DOS command.
Then a simple WinDiff should point you straight at the heart of the problem.
Luc Pattyn
|
|
|
|
|
I have revised my program to examine all the files on the C: drive. I have placed the files in a sorted list box.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
static int countFiles = 0;
public Form1()
{
InitializeComponent();
}
ArrayList list = new ArrayList();
void DirSearch(string sDir)
{
try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, "*.*"))
{
list.Add(f);
countFiles++;
}
DirSearch(d);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
listBox1.Items.Clear();
listBox1.Sorted = true;
list.Clear();
countFiles = 0;
DirSearch("c:\\");
foreach (string s in list) listBox1.Items.Add(s);
listBox1.Items.Add(" --> Number Of Files = " + countFiles);
this.Cursor = Cursors.Default;
}
}
}
When I run this code all directories begining with the letter 't' or more are NOT present this includes the "Windows" directory. I have now tested this on four different machines all of which report the same erroneus result. A directory called "system" is found and one called "text" is not.
|
|
|
|
|
Some ideas. maybe one of them triggers something:
- Are the missing names included in the ArrayList ? (is it listbox that failes ? or GetDirectories ?)
- do you have any special attributes set on the missing directories ?
- Are your different PCs cloned from a single one ?
- if directories show the problem, you can drop the GetFiles() stuff while debugging
- How does the "Windows" directory start with 't' ?
- does it also fail when starting with 'T' ? (filenames are supposed to be case insensitive)
- do you have anything special as Regional Settings, Culture, ... ?
Luc Pattyn
|
|
|
|
|
Some ideas. maybe one of them triggers something:
- Are the missing names included in the ArrayList ? (is it listbox that failes ? or GetDirectories ?)
The list box and the array list have the same number of files and content so it must be GetDirectories that is failing.
- do you have any special attributes set on the missing directories ?
No Special attributes or hidden
- Are your different PCs cloned from a single one ?
No Each one was an origional install from its own Windows disk.
- if directories show the problem, you can drop the GetFiles() stuff while debugging
I will check this
- How does the "Windows" directory start with 't' ?
Windows starts with 'W' which is more than 'T'
- does it also fail when starting with 'T' ? (filenames are supposed to be case insensitive)
I tried both upper and lower case and they both do the same "sWorld", "Sworld, will be found but not "tworld", "Tworld", or "Windows". I know this sounds weird but I have spent two days on this problem and I still dont have a clue. If you want post a message at electricfarm@gmail.com and I will send a Zip of the program and source. I tried to post it to Mediafire.com but they are having problems today.
- do you have anything special as Regional Settings, Culture, ... ?
|
|
|
|
|
One more idea: maybe you have some malware on your machine(s) that, so far, successfully
remained hidden; there exist such things that use what is known as a "root kit" to hide
themselves, basically by changing the internals of FindFirst/FindNext (on which dir
and GetDirectories must be based).
If it is, I would not expect that to hide all t* directories, just its own, but who knows.
I tried all kinds of strings in a System.Windows.Forms.Listbox, it does not care about
the string content, so it happily holds Temp and text folders.
Time to call the ghost busters ?
Luc Pattyn
|
|
|
|
|
This includes all directories 't-z'. Two of the machines have no modem so they should be clean. I am going to try using DirSearch recursivly on each directory on the root of the drive and see what happens!??
|
|
|
|
|
I think I have hit on a clue now, there may be a "System Volume Information" folder
(that is high up in the s-es, just before t) to which you dont have access
(something like UnauthorizedAccessException).
The one missing link now is how come you don't see such UnauthorizedAccessException ?
Luc Pattyn
|
|
|
|
|
Yes, I am pretty sure now, GetDirectories will return a "System Volume Information" folder
when such beast exists, but GetFile() will throw an UnauthorizedAccessException on it.
This exception gets mentioned in Visual's output pane as a "first chance ...exception..."
but I am not familiar with that, I'm still working with 1.1 most of the time.
BTW, you dont see some files/folders in Explorer, when you keep the default setting
of "hide protected operating system files (recommended)" checked.
IMHO software developers should better see all files at all times, so unchecking this
is one of the first things I do an a PC.
Luc Pattyn
|
|
|
|
|
Just another idea of implementation, not the best i'm afraid, cause it is slow to run, but maybe the most accurate since you require no specific programming. Sorry i only have a sample in VBA code, but with it you can compare your result and maybe determine where the error is:
<br />
With Application.FileSearch<br />
.NewSearch<br />
.LookIn = "c:\temp"<br />
.Filename = "*.txt"<br />
.MatchTextExactly = True<br />
.SearchSubFolders = True 'look in sub folders<br />
If .Execute > 0 Then<br />
For i = 1 To .FoundFiles.Count<br />
'do something with each file<br />
next<br />
end if<br />
David Domingues at webrickco@gmail.com. Feel free to visit http://webrick.athost.net/index.php
|
|
|
|