Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, October 07, 2008

winforms: Dock Fill is working wrong

I experienced much time with troubles in the C# windows form, if control is set dock:fill and I adding any other control (menu, status bar,whatever) to the form or panel.
In this case, main control with fill dock style doesn't recognize that need to fit, it overlapping any new control. Sometimes, to fix I creating new form and redesigned it from scratch.

Solution is very easy:
Right click on the control with fill dock style, and choose Bring to Front. Docking will be fixed :)

Source

Thursday, September 25, 2008

Performance Counters are damaged.

Found at the customer station that all perfomance counters are damaged and only numbers appears in the performance monitor app.
My application is failing when tried to use perfomance counters "Input string incorrect format".

Solution:
run command:
lodctr.exe /r

Performance monitor now ok, as well as my application too.

Monday, October 15, 2007

IIS7: Application pool, managed pipeline mode

Referenced posts:
Separated application pool
Application pool idenity

Windows Vista comes with new property of application pool..
Now we have property "managed pipeline mode".
It allows two options:
- 0 - Integrated (set by default during creation of the new application pool)
- 1 - Classic

In the .NET 3 new .NET library Microsoft.Web.Administration allows us to manage IIS settings very easy.

Unofortunately, I was required to turn IIS settings on the Windows Vista in old way, because software product still not working with .NET 3, so I use old good way of the ADSI and System.DirectoryServices

To set application pool in classic mode on Windows Vista/IIS7, I make additional call (in bold):
string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry pool = poolRoot.Children.Add(appPoolName, "IIsApplicationPool");
pool.InvokeSet("ManagedPipelineMode", new Object[] { 1 });
pool.CommitChanges();

I set 1, because I need to set my application pool working in classic mode. 0 should be used for integrated mode.

Wednesday, August 29, 2007

IIS6: Application pool Identity

Referenced post: Separated app pool

In referenced post I wrote how to create application pool and assign virtual directory for it..
Now I faced with requirement to change identity from default (Network service) to Local system account) to allow web application more permissions..

Property AppPoolIdentityType should help.
Following code will create pool and set Local System account identity for the pool:


string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry pool = poolRoot.Children.Add(appPoolName, "IIsApplicationPool");

pool.InvokeSet("AppPoolIdentityType", new Object[] { 0 });
pool.CommitChanges();


possible values:
0Local System
1Local Service
2Network Service
3Specific user


In the case of specific user need to use following additional operations:

pool.InvokeSet("WAMUserName", new Object[] { computerName + @"\" + user });
pool.InvokeSet("WAMUserPass", new Object[] { password });

Tuesday, August 21, 2007

Sql Server 913 error: Could not find database ID

Strange behaviour of sql server I found.
When databaseid is changed in the sysdatabases, but name of database is same I get 913 error on some queries:
Could not find database ID %d. Database may not be activated yet or may be in transition.
It is totally thrue, my database have another id after series of drop/create, but somehow .NET SqlConnection remember it. There is no matter, if I am closing and reopen connection. I even did SqlConnection.ClearAllPools()
Even creating new connection with same connection string doesn't help. And it is not bug referencing by MSDN if I am using user-defined function. Nope, I don't have UDR in query.. But error appears in some specific queries, like Create mydb.dbo.mytable, or update, or insert..
One note.. I always use connection string with default database master
After drop/create database, I changed connection string and set default database name to the just created.

Bingo!!! It works!
But why?? Why connection of master, even with clearing pools, remember obsolete information from sysdatabases, if I even recreate new connection on it?
Didn't found any reasonable answer.

Wednesday, July 11, 2007

IIS6: separated application pool

When IIS runs multiple versions of .NET web projects, collision happens sometimes.
So, I required to assign my web project in separated application pool under Windows 2003 /IIS6 programmatically.

It is easy through System.DirectoryServices of .NET
There is, I create virtual directory "myWebDir" and assign it to new pool with the name "myAppPool":


DirectoryEntry folderRoot = new DirectoryEntry("IIS://localhost/W3SVC/1/Root");
DirectoryEntry virDir = folderRoot.Children.Add("myWebDir", "IIsWebVirtualDir");
object[] param ={ 0, "myAppPool", true };
virtDir.Invoke("AppCreate3", param);
//set virtual directory properties
//.......
newVirDir.CommitChanges();




Application pool is created automatically if third parameter is a true in the param array.

However, I would like also to manage my application pools without specific virtual directory.
Here are examples to create/remove application pools:

Create Application pool:


string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
if (!DirectoryEntry.Exists(IISAppPoolRootPath + "/" + appPoolName);
{
DirectoryEntry pool = poolRoot.Children.Add(appPoolName, "IIsApplicationPool");
pool.CommitChanges();
}





Remove Application pool:


string appPoolName = "myAppPool";
DirectoryEntry poolRoot = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
DirectoryEntry pool = poolRoot.Children.Find(appPoolName, "IIsApplicationPool");
if (pool != null)
{
poolRoot.Children.Remove(pool);
poolRoot.CommitChanges();
}




Remove only works, if no one web application assigned to pool.

Sunday, February 04, 2007

Access common windows folders from C#

I interested to get desktop and programs folder from C# application to create some shortcuts there. .NET allows it in following way:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Environment.GetFolderPath(Environment.SpecialFolder.Programs);

But results are directories for the current user, like

c:\document And Settings\LastDon\Destkop.

However I want to place shortcuts for all users, for profile ALLUSERS.
So, I found registry key

HKEY_LOCAL_MACHINE\SOFTWARE\microsoft\windows\currentversion\explorer\shell folders

Not too happy - Windows 2003 is not using this key, and anyway, it is not recommended by microsoft.
So, I come to solution by using API Shell32.dll:



[DllImport("Shell32.DLL")]
public static extern int SHGetSpecialFolderLocation(
IntPtr hwndOwner, int nFolder, out IntPtr ppidl);

[DllImport("Shell32.DLL")]
public static extern int SHGetPathFromIDList(
IntPtr pidl, StringBuilder Path);

const int CSIDL_COMMON_PROGRAMS = 23;
const int CSIDL_COMMON_STARTUP = 24;
const int CSIDL_COMMON_DESKTOPDIRECTORY = 25;

private string GetSpecialFolder(int folderType)
{
IntPtr pResultList;
IntPtr handlePtr = new IntPtr(0);
int result =SHGetSpecialFolderLocation(handlePtr, folderType, out pResultList);
if (result < 0)
{
return null;
}
StringBuilder sb = new StringBuilder (500);
result = SHGetPathFromIDList(pResultList, sb);
if (result == 0) // boolean result
{
return null;
}
return sb.ToString();
}




Get destkop path:
GetSpecialFolder(CSIDL_COMMON_DESKTOPDIRECTORY);
Get programs path:
GetSpecialFolder(CSIDL_COMMON_PROGRAMS);

Following microsoft article helped me to solve it out
http://support.microsoft.com/kb/306285