virtual directory properties in c# - c#

I did a quick search and got this link
From StackOverflow
I get this error message
"Unknown error (0x80005000)"
when it hits
bool canCreate = !(schema.Properties["Syntax"].Value.ToString().ToUpper() == "BOOLEAN");
My requirement is to create a virtual directory and set AccessRead, IsolationMode, Scripts and Executables, ApplicationProtection to medium.
Got it to work, here is the code:
Note: I still couldn't get it to work on windows 7, it throws the same error and the message includes reference to System.InterOp... At the moment I don't care about getting it to work on windows 7, I deployed it on windows 2003, it works.
public void CreateVirtualDirectory(string virtualdirectory, string physicalpath)
{
try
{
///check if path exists
if (!Directory.Exists(physicalpath))
{
Log(string.Format(#"CreateVirtualDirectory; Path not found - {0}", physicalpath));
return;
}
DirectoryEntry parent = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/1/Root", Environment.MachineName));
foreach (System.DirectoryServices.DirectoryEntry v in parent.Children)
{
if (v.Name == virtualdirectory)
{
try
{
parent.Invoke("Delete", new string[] { v.SchemaClassName, virtualdirectory });
}
catch
{
}
}
}
DirectoryEntry newFolder = (DirectoryEntry)parent.Invoke("Create", "IIsWebVirtualDir", virtualdirectory);
newFolder.InvokeSet("Path", physicalpath);
newFolder.Invoke("AppCreate", false);
newFolder.InvokeSet("AppFriendlyName", virtualdirectory);
const int MEDIUM_POOL = 2;
newFolder.Properties["AccessRead"][0] = true;
newFolder.Properties["AccessExecute"][0] = true;
newFolder.Properties["AccessWrite"][0] = false;
newFolder.Properties["AccessScript"][0] = true;
newFolder.Properties["AuthNTLM"][0] = true;
newFolder.Properties["AppIsolated"].Clear();
newFolder.Properties["AppIsolated"].Add(MEDIUM_POOL);
newFolder.CommitChanges();
}
catch (Exception ex)
{
Log(string.Format(#"CreateVirtualDirectory '{0}' failed; {1}", virtualdirectory, ex.Message));
}
}

You're using DirectoryEntry to create websites which requires IIS Compat Mode to be installed/configured in IIS. That's compat mode for the IIS6 directory entry interfaces if you're on IIS7 or later.
If it's not installed, you'll get 80005000.
If you're on IIS7 or later (Windows Server 2008 or Vista and later), the newer (non-compat mode) approach is the new managed Microsoft.Web.Administration.
http://blogs.msdn.com/b/carlosag/archive/2006/04/17/microsoftwebadministration.aspx

Related

ServerManager fails with 0x80040154 in c# Winforms app creating simple web application in IIS

I am writing a small app that installs IIS and configures a website before deploying the files to it.
On a freshly reset Windows 10, the first attempt always fails with the 0x80040154 COM+ component failure as documented in This question
I looked at the version I am using and it is the latest and correct one for .net standard (4.8) and not the one meant for .net core
When I press the button to rerun the function it always finishes correctly. I tried using a retry routine, and it fails on each retry, yet runs fine again when the button is pressed. The reason for this I assume is that the server manager object isn't disposed when it hits the catch block since its in a using statement.
I can work around that, but I really want to understand the issue and make a permanent fix.
My routine simply creates a website in IIS and creates an app pool to assign to it.
And it is running with elevated privileges
For reference:
Machine is Windows 10 latest from the downloadable media creator.
Microsoft.Web.Administrator version is 7.0.0.0
App is .net 4.8 standard windows forms
using (var serverManager = new ServerManager())
{
string iisrootdir = drive;
//Check for inetpub/wwwroot
if (!Directory.Exists(iisrootdir)) //Check for Drive D
{
iisrootdir = #"C:\";
}
string iiscmsdir = Path.Combine(iisrootdir, "webdir", "appdir");
if (!Directory.Exists(iiscmsdir))
Directory.CreateDirectory(iiscmsdir);
var settings = new ApplicationSettings();
settings.ReadFromFile();
settings.CMSPATH = iiscmsdir;
settings.SaveToFile();
try
{
string poolName = "DefaultAppPool";
if (serverManager.Sites.Count > 0)
{
Site myDefualtWebsite = serverManager.Sites[0];
if (myDefualtWebsite != null)
{
OnRaiseInstallEvent(new InstallEventArgs("CreateWebsite", ProcessState.Started,
"Remove Default Website"));
serverManager.Sites.Remove(myDefualtWebsite);
serverManager.CommitChanges();
}
}
if (!WebsiteExists("sitename"))
{
mySite.ServerAutoStart = true;
}
Site site = serverManager.Sites["sitename"];
if (!AppPoolExists(poolName))
{
serverManager.ApplicationPools.Add(poolName);
}
ApplicationPool apppool = serverManager.ApplicationPools[poolName];
apppool.ManagedPipelineMode = ManagedPipelineMode.Integrated;
apppool.ManagedRuntimeVersion = "";
serverManager.Sites["sitename"].ApplicationDefaults.ApplicationPoolName = poolName;
foreach (var item in serverManager.Sites["sitename"].Applications)
{
item.ApplicationPoolName = poolName;
}
serverManager.CommitChanges();
apppool.Recycle();
serverManager.CommitChanges();
}
catch (Exception ex)
{
if (ex.Message.Contains("80040154") && errorCount < 4)
{
if (serverManager != null)
serverManager.Dispose();
errorCount++;
OnRaiseInstallEvent(new InstallEventArgs("CreateWebsite", ProcessState.Started,
"Error encountered with COM+ object, trying again: " + errorCount));
CreateWebsite(#"D:\");
}
else
{
if (serverManager != null)
serverManager.Dispose();
errorCount = 0;
OnRaiseErrorEvent(new InstallErrorEventArgs("CreateWebsite", ProcessState.Error, ex));
return false;
}
}
finally
{
serverManager?.Dispose();
}
Thanks for the help Guys. I found the problem.
DISM was running in its own thread. The Process object exited the moment it launched. My function was then attempting to configure IIS before it had finished installing.

OpenSubKey() doesn't contain installed Application that I can see in regedit.exe

I wrote an In/Uninstaller for a custom installation for MongoDb.
My way to install the mongoDB ist the following.
private Process GetInstallMongoProcess() {
Log("Installing MongoDB Version 3.4.10 ...");
Process installProcess = new Process();
installProcess.StartInfo = new ProcessStartInfo("msiexec.exe", $#"/q /Lie {Constants.LogFileName} /i mongodb-win32-x86_64-2008plus-ssl-3.4.10-signed.msi INSTALLLOCATION=""{Constants.Path}3.4\"" ADDLOCAL=""all""");
installProcess.OutputDataReceived += OnInstallProcessDataReceived;
installProcess.Start();
return installProcess;
}
But then I need to check of this Application is installed. The way I do is
private bool CheckIfAlreadyInstalled()
{
string[] registryKeys = { #"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", #"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" };
bool appIsInstalled = false;
foreach (string registryKey in registryKeys) {
RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey);
if (key != null) {
List<string> listOfInstalledApplications = key.GetSubKeyNames().ToList();
appIsInstalled = listOfInstalledApplications.Contains(ApplicationGuid);
}
}
if (appIsInstalled) {
Log("MongoDB is already installed");
}
return appIsInstalled;
}
I can't find my installed application. Neither the name nor the ApplicationGuid. I get about 300 entries, but the entry I need is not present, but I can See the installed Software in Regedit.exe
Do I am searching in wrong node ??

MSFT_Volume Format Method, in WinPE

I hope there may be some insight and maybe help in relation to my query.
I am trying to write the last module to a customised Windows Recovery environment, based on WinPE for Windows 10.
The solution currently utilises DiskPart to create the Disk Partitions (inline with Microsoft advice here), with the change of not providing for the WinRE partition.
After a fair amount of research and tinkering with a project found on MSDN, I managed to get a working project that would clear/partition and format a disk using WMI (MSFT_Disk,MSFT_Partition & MSFT_Volume). That is fully working within Windows 10 on a virtual disk of 15 GiB size.
When I tried it in WinPE (as this will be the OS that it will be used on), it failed on multiple elements, even though the Methods were reported as there). (MSFT_Disk::Clear, MSFT_Volume::Format).
On inspecting the result of my work in DiskPart, I noticed that even though MSFT_Volume::Format had a return value of 2 "Unknown Error" it had actually formatted the data partition (NTFS, ClusterSize 4096). However, when when the Format method is applied to the ESP (FAT32, cluster size 512/1024/4096)it fails fully, with the FileSystem still being reported as RAW, but will apply an AccessPath. "ExtendedStatus" only returns Error 2 (unless I am not accessing it correctly).
Has anyone else had this problem and managed to rectify the problem? Multiple Google searches have thrown out the idea that there maybe a WMI error, but within PowerShell, not as coded in C#/C++. Below are some code snippets and screen shots:
Creating the Partition:
try
{
parameters = disk.GetMethodParameters("CreatePartition");
}
catch (Exception e)
{
Console.WriteLine("Exception in Line 88: " + e.Message);
}
if (PartitionType != "PRIMARY")
{
FillInvocationParameters(parameters, new Dictionary<string, object> { { "Size", _partitionSize.ToString() },
{ "AssignDriveLetter", false },
{ "GpTType", _partitionType} });
}
else
{
FillInvocationParameters(parameters, new Dictionary<string, object> { { "UseMaximumSize", true },
{ "AssignDriveLetter", false },
{ "GpTType", _partitionType} });
}
try
{
res = disk.InvokeMethod("CreatePartition", parameters, null);
objresult = res["ReturnValue"]; //write error handliong routine for the objResult.
Int32.TryParse(objresult.ToString(), out intRes);
}
catch (Exception e)
{
Console.WriteLine("Exception in Line 146: " + e.Message);
ErrorID = Marshal.GetLastWin32Error();
return false;
}
if (intRes != 0)
{
Console.Write("CreatePartition {0} failed with the result: {1} Line 111.", PartitionType, objresult.ToString());
Console.ReadKey();
ErrorID = Marshal.GetLastWin32Error();
return false;
}
//this is the format point for EFI System Disk.. MSFTPARTITIONtoVOLUME MSFT VOLUME::FORMAT
Console.Write($"{PartitionType} Partition has been created\r\n");
string partition = ((ManagementBaseObject)res["CreatedPartition"])["__PATH"] as string;
var MSFT_Partition = new ManagementObject(#"root\Microsoft\Windows\Storage", partition, null);
var partitionIndex = partition.IndexOf(#"ObjectId=\");
partition = partition.Substring(partitionIndex);
partitionIndex = partition.IndexOf(#"}\\");
partitionIndex = partitionIndex + 1;
partition = partition.Substring(0, partitionIndex);
var strMSFTPartition = partition;
partition = partition.Replace("root", "ROOT");
var partitionGuid = MSFT_Partition["Guid"] as string;
Console.WriteLine("Line 138: New Partition GUID: " + partitionGuid);
Parameters is declared as:
ManagementBaseObject parameters = null;
FillInvokationParamters:
private static void FillInvocationParameters(ManagementBaseObject InvocationParameters, IDictionary<string, object> parameters)
{
foreach (var pair in parameters)
{
string stringParamValue;
var managementObjectParam = pair.Value as ManagementObject;
var arrayParam = pair.Value as string;
if (managementObjectParam != null)
{
stringParamValue = managementObjectParam.GetText(TextFormat.CimDtd20);
InvocationParameters[pair.Key] = stringParamValue;
}
else if (arrayParam != null)
InvocationParameters[pair.Key] = arrayParam;
else if (pair.Value != null)
{
stringParamValue = pair.Value.ToString();
InvocationParameters[pair.Key] = stringParamValue;
}
}
}
And the Format Method call:
try
{
Console.Write("Line 174: Attempting to Format with MSFT_Volume::Format FileSystem {0}, Label: {1}, ClusterSize: {2}\r\n", _FileSystem, _VolumeLable, _allocationUnit.ToString());
parameters = MSFTVolume.GetMethodParameters("Format");
FillInvocationParameters(parameters, new Dictionary<string, object>{ { "FileSystem", _FileSystem },
{ "AllocationUnitSize", _allocationUnit },
{ "FileSystemLabel", _VolumeLable },
{ "Full", false} });
res = MSFTVolume.InvokeMethod("Format", parameters, null);
objresult = res["ReturnValue"];
Int32.TryParse(objresult.ToString(), out intReslt);
}
catch (Exception e)
{
Console.WriteLine("Line: 189 The Following error occured while attmpeting to Format the Volume: " + e.Message);
ErrorID = Marshal.GetLastWin32Error();
return false;
}
For the ESP the following applies:
switch(PartitionType)
{
case "EFI":
{
_partitionType = "{c12a7328-f81f-11d2-ba4b-00a0c93ec93b}";
_partitionSize = 260 /*bytes*/ *1024 /*Kilobytes*/ *1024 /*Megabytes*/;
_FileSystem = "FAT32";
_allocationUnit = 1024;
_VolumeLable = "System";
break;
}
}
Screenshots of my console app and diskpart results:
Format Disk Console output
DiskPart Results
Any help/insights will be gratefully appreciated.
Regards
Richie
Note that there are several builds of the ADK for newer builds of Windows 10...be sure to use the latest and greatest. We had issues similar to yours with both dism.exe and diskpart.exe. At one point, (on early win 10 adks) we got dism.exe and diskpart.exe from an 8.1 adk. Hacky as heck, but you gotta do what you gotta do :-)
Update:
Checked with the support group...turns out, I got the versions mixed up. The very first win 10 adk had a working dism and diskpart (adk for windows 10 1503)...and we've been unable to use newer ones from within PE since...and so we're still deploying a new winpe from new ADK - but copying in the diskpart and dism saved off from the 1503 adk. We never deployed 8.1 versions - my bad. Still - hacky as all git out.
Best we can recollect, it only presented a problem in one side of the house...either BIOS+MBR or UEFI+GPT...but none of us remember which.

ClickOnce CheckForUpdate() returns null after a while

I have a problem with a ClickOnce application, when I call CheckForUpdate() for a while it works, and I the restart system works perfectly. After about one hour it starts crashing. I'm running this on a seperate thread and the ClickOnce application is on our local network.
The error code:
System.Deployment.Application.DeploymentException: An application for this deployment is already installed with a different application identity. at System.Deployment.Application.SubscriptionStore.CheckAndReferenceApplication(SubscriptionState subState, DefinitionAppId appId, Int64 transactionId)
at System.Deployment.Application.DeploymentManager.BindCore(Boolean blocking, TempFile& tempDeploy, TempDirectory& tempAppDir, FileStream& refTransaction, String& productName)
at System.Deployment.Application.DeploymentManager.Bind()
at System.Deployment.Application.ApplicationDeployment.CheckForDetailedUpdate(Boolean persistUpdateCheckResult)
at System.Deployment.Application.ApplicationDeployment.CheckForUpdate()
Here is the method:
private void RestartUpdate()
{
bool running = true;
while (running)
{
Thread.Sleep(5000);
try
{
if (!RESTARTING)
{
if (ApplicationDeployment.IsNetworkDeployed)
{
ApplicationDeployment updateCheck = ApplicationDeployment.CurrentDeployment;
bool newUpdate = updateCheck.CheckForUpdate(); **<---- Problem**
if (newUpdate == true)
{
RESTARTING = true;
updateCheck.UpdateCompleted +=
new AsyncCompletedEventHandler(
Deployment_UpdateCompleted);
updateCheck.UpdateAsync();
}
}
}
}
catch (Exception e)
{
SendErrorMessageToServer(e.ToString());
}
}
}
Do you have any clue why this is happening?
EDIT:
Found an answer from James Miles who seems to bypass the click-once deployment API entirely when checking for updates:
//Used to use the Clickonce API but we've uncovered a pretty serious bug which results in a COMException and the loss of ability
//to check for updates. So until this is fixed, we're resorting to a very lo-fi way of checking for an update.
var manifestFile = new WebClient().DownloadString(updateLocation);
var xdoc = XDocument.Parse(manifestFile);
XNamespace nsSys = "urn:schemas-microsoft-com:asm.v1";
var version = new Version(xdoc.Descendants(nsSys + "assemblyIdentity").First().Attribute("version").Value);
I went for the solution that bypasses the OneClick API.
string manifestFile = new WebClient().DownloadString(#"\\*\*.application");
XDocument xdoc = XDocument.Parse(manifestFile);
XNamespace nsSys = "urn:schemas-microsoft-com:asm.v1";
Version versionNew = new Version(xdoc.Descendants(nsSys + "assemblyIdentity").First().Attribute("version").Value);
int result = applicationDeplayment.CurrentVersion.CompareTo(versionNew);
if (result < 0)
{
applicationDeplayment.UpdateAsync();
}

Getting location of file tnsnames.ora by code

How can I get the location of the tnsnames.ora file by code, in a machine with the Oracle client installed?
Is there a windows registry key indicating the location of this file?
Some years ago I had the same problem.
Back then I had to support Oracle 9 and 10 so the code only takes care of those versions, but maybe it saves you from some research.
The idea is to:
search the registry to determine the oracle client version
try to find the ORACLE_HOME
finally get the tnsnames from HOME
public enum OracleVersion
{
Oracle9,
Oracle10,
Oracle0
};
private OracleVersion GetOracleVersion()
{
RegistryKey rgkLM = Registry.LocalMachine;
RegistryKey rgkAllHome = rgkLM.OpenSubKey(#"SOFTWARE\ORACLE\ALL_HOMES");
/*
* 10g Installationen don't have an ALL_HOMES key
* Try to find HOME at SOFTWARE\ORACLE\
* 10g homes start with KEY_
*/
string[] okeys = rgkLM.OpenSubKey(#"SOFTWARE\ORACLE").GetSubKeyNames();
foreach (string okey in okeys)
{
if (okey.StartsWith("KEY_"))
return OracleVersion.Oracle10;
}
if (rgkAllHome != null)
{
string strLastHome = "";
object objLastHome = rgkAllHome.GetValue("LAST_HOME");
strLastHome = objLastHome.ToString();
RegistryKey rgkActualHome = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\ORACLE\HOME" + strLastHome);
string strOraHome = "";
object objOraHome = rgkActualHome.GetValue("ORACLE_HOME");
string strOracleHome = strOraHome = objOraHome.ToString();
return OracleVersion.Oracle9;
}
return OracleVersion.Oracle0;
}
private string GetOracleHome()
{
RegistryKey rgkLM = Registry.LocalMachine;
RegistryKey rgkAllHome = rgkLM.OpenSubKey(#"SOFTWARE\ORACLE\ALL_HOMES");
OracleVersion ov = this.GetOracleVersion();
switch(ov)
{
case OracleVersion.Oracle10:
{
string[] okeys = rgkLM.OpenSubKey(#"SOFTWARE\ORACLE").GetSubKeyNames();
foreach (string okey in okeys)
{
if (okey.StartsWith("KEY_"))
{
return rgkLM.OpenSubKey(#"SOFTWARE\ORACLE\" + okey).GetValue("ORACLE_HOME") as string;
}
}
throw new Exception("No Oracle Home found");
}
case OracleVersion.Oracle9:
{
string strLastHome = "";
object objLastHome = rgkAllHome.GetValue("LAST_HOME");
strLastHome = objLastHome.ToString();
RegistryKey rgkActualHome = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\ORACLE\HOME" + strLastHome);
string strOraHome = "";
object objOraHome = rgkActualHome.GetValue("ORACLE_HOME");
string strOracleHome = strOraHome = objOraHome.ToString();
return strOraHome;
}
default:
{
throw new Exception("No supported Oracle Installation found");
}
}
}
public string GetTNSNAMESORAFilePath()
{
string strOracleHome = GetOracleHome();
if (strOracleHome != "")
{
string strTNSNAMESORAFilePath = strOracleHome + #"\NETWORK\ADMIN\TNSNAMES.ORA";
if (File.Exists(strTNSNAMESORAFilePath))
{
return strTNSNAMESORAFilePath;
}
else
{
strTNSNAMESORAFilePath = strOracleHome + #"\NET80\ADMIN\TNSNAMES.ORA";
if (File.Exists(strTNSNAMESORAFilePath))
{
return strTNSNAMESORAFilePath;
}
else
{
throw new SystemException("Could not find tnsnames.ora");
}
}
}
else
{
throw new SystemException("Could not determine ORAHOME");
}
}
On Windows, the most likely locations are either %ORACLE_HOME%/network/admin or %TNS_ADMIN% (or the TNS_ADMIN registry setting). These two cover almost every installation.
Of course it is possible to have a working Oracle client without this file. Oracle has bewildering array of networking options, and there are plenty of ways to achieve a working setup with using TNSNAMES. Depending on what you are trying to achieve here, your first port of call might be the sqlnet.ora file, which is also found in %ORACLE_HOME%/network/admin. This should contain a line that looks something like this:
NAMES.DIRECTORY_PATH= (LDAP, TNSNAMES, HOSTNAME)
TNSNAMES means it will use the TNSNAMES.ora file (second in this case). LDAP and HOSTNAME are alternate ways of resolving the database. If there is no TNSNAMES the TNSNAMES.ora file will be ignored if it exists in the right place.
In C# / .NET this should get you the environment variables:
Environment.GetEnvironmentVariable("ORACLE_HOME");
Environment.GetEnvironmentVariable("TNS_ADMIN");
List<string> logicalDrives = Directory.GetLogicalDrives().ToList();
List<string> result = new List<string>();
foreach (string drive in logicalDrives)
{
Console.WriteLine("Searching " + drive);
DriveInfo di = new DriveInfo(drive);
if(di.IsReady)
result = Directory.GetFiles(drive, "tnsnames.ora", SearchOption.AllDirectories).ToList();
if (0 < result.Count) return;
}
foreach (string file in result) { Console.WriteLine(result); }
According to the net that depends on the version of Oracle and the working directory of the SQL*Plus process. This first link tells you the environment variable that specifies the base path for some versions (7, 8, 9i) of Oracle. If you use a different one, I'm sure there's a similar way to get to the system directory.
If you spread versions of these files all over the place though and rely on the "look for a local tnsnames.ora first" behaviour of the client, then I guess you're out of luck.
I'm not a C# or a Windows guy for that matter so hopefully this helps. The tnsnames.ora file should be located in:
ORACLE_HOME\network\admin
If an alternate location has been specified, it should be available via the TNS_ADMIN registry key.
See this link for more information on how Oracle handles tns names on Windows.

Categories