Drew Barfield

Hello All,

I'm a new member - I just signed up today. I hope this is the place to finally get an answer to this question.
If the following method is called with "temp" as the argument, it is supposed to list all folders that contain
the "temp" in it's path (i.e., "C:\Temp", "C:\Windows\Temp", ...). However, on Windows Vista the method
returns only network drive folders (my wife's laptop), and some folders on my second partition (i.e., "D:\").

Did MS neuter WMI for Vista Is it possible (without more registry/group policy hacking) to make it work
I have already tried giving my application full permissions via "Once Click". I also edited the manifest
manually with no results.

public static void EmptyFoldersByName(string strName)
{

try
{

// Execute WMI Query and wait for result
WqlObjectQuery wqlQuery = new WqlObjectQuery
("SELECT Name FROM CIM_Directory WHERE FileName = '" + strName + "'");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(wqlQuery);
ManagementObjectCollection collFolders = searcher.Get();

// Display each folder path...
foreach (ManagementObject folder in collFolders)
{

string strCurrentPath = folder.Properties["Name"].Value.ToString();
MessageBox.Show( strCurrentPath );
}

}
catch (Exception ex)
{

MessageBox.Show(ex.ToString());

}

}

Thanks in Advance!



Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Windows Vista was the best thing to ever happen to Apple. I know it has only been out a month or so but it will go down in history in the same category as MS Bob:

string[] strlogicalDrives = Directory.GetLogicalDrives();

foreach (string strDrive in strlogicalDrives)
{

string[] strFolders = Directory.GetDirectories(strDrive,
"temp", SearchOption.AllDirectories);


foreach (string strFolder in strFolders)
{
listBox1.Items.Add(strFolder);
}

}

ERROR!
ACCESS DENIED!




Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

AndyCadley

WMI can only enumerate hidden that the user has permission to access. If you want to enumerate the entire drive you'll need to have a manifest with the 'requireAdministrator' level to force UAC to run the process as a full administrator. Note that this is different to Full Trust code access security permissions.

lf manually traversing the drive, you need to be aware that some folders aren't accessible and include code to deal with the resultant UnauthorizedAccessException exceptions.





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Bruce N. Baker - MSFT

Just out of curiosity, why are you using WMI to get temp directories

I would suggest using Environment . As far a UAC goes, it goes without saying that this is a very good move to increased security. But as all security tighting proves, it carries with it some extra insight on the part of the developer to work with it and to work without requiring admin priviledges whenever possible.

On a side note, execute your program with run as admin to see if that makes a difference in your results.






Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Hi Bruce,

I'm converting a WSH script into C#. The script was titled "Barfield Cleaner" and I used it to keep my XP system up and running.

The EmptyAllFoldersByName function uses WMI so I can easily get a list of all folders of the same name from all drives (network included), and partitions. In script WMI was the best choice: One SQL-like query returned what I wanted and many interesting properties came with it. I call this function with numerous arguments ("temp", "tmp", "cache", "debug", etc...). The function normally returns tens of folders that can be easily and harmlessly emptied.

I do the same with another function DeleteAllFilesByName (passing in "log", "bak", "old", "pdb", etc.)

This script was originally written to ensure my father's desktop would keep running while I was in basic trainning (Navy) but since then I've used it as a scheduled task and kept adding to it.

Now, I've recently "upgraded" to Vista and have to rewrite the cleaner because most of the registry tweaks in it don't work on Vista. So, I may as well make it a true binary...





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Its official (for me) - MS did neuter WMI as well as C#.

The manifest and RC files did nothing but enable the "Run as Administrator" check
box in the Shortcut properties dialog. I already have a true Administrator account. Checking the box and clicking "Run as Administrator" did no good.

Both code snippets run exactly the same. I opened the exe in Notepad and noted the given manifest (requireAdministrator)- so it was built correctly.

I give up - its not worth the effort. Besides my school (Univ. of NC at Wilmington) has already passed an official moratorium on upgrading until 2009. Maybe MS can fix this thing by then.

Thank you both for your help!




Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

AndyCadley

You should be able to enumerate directory entries with something a bit like this:

public void EnumerateDirectorys(string root)

{

try

{

string[] strAllSubDirs = Directory.GetDirectories(root);

if (System.IO.Path.GetFileName(root) == "temp")

{

listBox1.Items.Add(root);

}

foreach (string subdir in strAllSubDirs)

{

EnumerateDirectorys(subdir);

}

}

catch (UnauthorizedAccessException)

{

//ignore

}

catch (IOException)

{

//ignore

}

}

which should be pretty much immune to LUA issues. You might want to spruce it up with RegEx's for the filename matching but it should at least give you a start.





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Andy,

Thank you. I'll try this when I get home tonight. I'm concerned though that folders after the exception won't get listed. Or do 'Access Denied Exceptions' get thrown after the GetDirectories internal loop has completed

I can't help it: I'm determined to figure out how to get applications to run on vista.




Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Why does this work I find "C:\windows\Temp" in the listbox as clear as day!
I must be missing something big...

try
{
string[] strFolders = Directory.GetDirectories("C:\\windows");
listBox1.BeginUpdate();
listBox1.Items.AddRange(strFolders);
listBox1.EndUpdate();
}
catch (Exception)
{
}

Thanks Andy!





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

Drew Barfield

Success! The total solution is given below:
Thanks everyone!

public void EmptyAllFoldersByName(string root, string name)
{
try
{

string[] strAllSubDirs = Directory.GetDirectories( root );

if ( Path.GetFileName(root).ToLower() == name.ToLower() )
{
listBox1.Items.Add(root);
DeleteAllFiles(root);
DeleteAllFolders(root);
}

foreach (string subdir in strAllSubDirs)
{
EmptyAllFoldersByName(subdir, name);
}

}
catch (UnauthorizedAccessException)
{
//ignore
}
catch (IOException)
{
//ignore
}
}

private void DeleteAllFiles(string path)
{

foreach (string strFile in Directory.GetFiles(path))
{

try
{
File.Delete(strFile);
}
catch (Exception)
{
}
}

}

private void DeleteAllFolders(string path)
{
foreach (string strFolder in Directory.GetDirectories(path))
{

try
{
DeleteAllFiles(strFolder);

if ( Directory.GetDirectories(strFolder).Length > 0)
{
DeleteAllFolders(strFolder);
}

Directory.Delete(strFolder);
}
catch (Exception)
{
}
}
}

private void button1_Click(object sender, EventArgs e)
{
string[] strDrives = Directory.GetLogicalDrives();

foreach (string strDrive in strDrives)
EmptyAllFoldersByName(strDrive,
"temp");
}





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

AndyCadley

Drew Barfield wrote:

Why does this work

Just to answer that last one, it works because the Exceptions will be thrown by Directory.GetDirectories if you are within a directory that you don't have permission to enumerate. Recursion is used to make sure all the directories that we find get visited, regardless of whether we hit security issues when we do try them.

Glad to see you've got it working though!





Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

darling

Drew Barfield wrote:
Hello All,

I'm a new member - I just signed up today. I hope this is the place to finally get an answer to this question.
If the following method is called with "temp" as the argument, it is supposed to list all folders that contain
the "temp" in it's path (i.e., "C:\Temp", "C:\Windows\Temp", ...). However, on Windows Vista the method
returns only network drive folders (my wife's laptop), and some folders on my second partition (i.e., "D:\").

Did MS neuter WMI for Vista Is it possible (without more registry/group policy hacking) to make it work
I have already tried giving my application full permissions via "Once Click". I also edited the manifest
manually with no results.

public static void EmptyFoldersByName(string strName)
{

try
{

// Execute WMI Query and wait for result
WqlObjectQuery wqlQuery = new WqlObjectQuery
("SELECT Name FROM CIM_Directory WHERE FileName = '" + strName + "'");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(wqlQuery);
ManagementObjectCollection collFolders = searcher.Get();

// Display each folder path...
foreach (ManagementObject folder in collFolders)
{

string strCurrentPath = folder.Properties["Name"].Value.ToString();
MessageBox.Show( strCurrentPath );
}

}
catch (Exception ex)
{

MessageBox.Show(ex.ToString());

}

}

Thanks in Advance!

no idea man






Re: Application Compatibility for Windows Vista WMI does not return all CIM_Directory entries ?

darling

Drew Barfield wrote:
Hello All,

I'm a new member - I just signed up today. I hope this is the place to finally get an answer to this question.
If the following method is called with "temp" as the argument, it is supposed to list all folders that contain
the "temp" in it's path (i.e., "C:\Temp", "C:\Windows\Temp", ...). However, on Windows Vista the method
returns only network drive folders (my wife's laptop), and some folders on my second partition (i.e., "D:\").

Did MS neuter WMI for Vista Is it possible (without more registry/group policy hacking) to make it work
I have already tried giving my application full permissions via "Once Click". I also edited the manifest
manually with no results.

public static void EmptyFoldersByName(string strName)
{

try
{

// Execute WMI Query and wait for result
WqlObjectQuery wqlQuery = new WqlObjectQuery
("SELECT Name FROM CIM_Directory WHERE FileName = '" + strName + "'");
ManagementObjectSearcher searcher =
new ManagementObjectSearcher(wqlQuery);
ManagementObjectCollection collFolders = searcher.Get();

// Display each folder path...
foreach (ManagementObject folder in collFolders)
{

string strCurrentPath = folder.Properties["Name"].Value.ToString();
MessageBox.Show( strCurrentPath );
}

}
catch (Exception ex)
{

MessageBox.Show(ex.ToString());

}

}

Thanks in Advance!

no idea man