Deleting profile folder through UNC path with WMI? -
12-10-2007
, 09:27 AM
Hello,
I want to delete user profiles with C# code and running into the
problem that some files in the profile are read only.
Therefor, I want to use WMI to delete the profile folder, but somehow
I can't find the correct path string to use. I don't want to use a
drive letter for accessing the folder to delete, but an unc path.
Can someone tell me what I'm doing wrong?
Thanks in advance,
Sandra
My code:
private void button3_Click(object sender, EventArgs e)
{
DeleteProfileFolder(@"\
\CP-1001VS03\TSPROFILE204\ML00033");
}
private bool DeleteProfileFolder(string Path)
{
bool result = false;
try
{
string path = @"Win32_Directory.Name='" + Path + "'";
ManagementObject mo = new ManagementObject(path);
mo.Get();
ManagementBaseObject outParams =
mo.InvokeMethod("Delete", null, null);
if ((uint)(outParams.Properties["ReturnValue"].Value) !
= 0)
{
AddToLog("-- ERROR deleting profile folder [" +
Path + "] with return value: " +
outParams.Properties["ReturnValue"].Value.ToString());
}
else
{
result = true;
}
}
catch (Exception ex)
{
AddToLog("-- ERROR deleting profile folder [" + Path +
"]: " + ex.Message);
}
return result;
} |