Tuesday, February 22, 2011

Center a Control on a Form in .NET

It turns out to be very easy to center a control on a .NET form. Just un-anchor the control from any side and it will center from its position. If you just un-anchor from the left and right it will center horizontally, and if you just un-anchor from the top and bottom, it will center vertically. Un-anchor from all sides and it will center on the form.

More info at:

http://stackoverflow.com/questions/491399/c-centering-controls-within-a-form-in-net-winforms

Friday, February 11, 2011

How solve the Problem of loading a Native DLL when using ASP.NET

I have had the problem of getting ASP.NET to use a Native DLL for years. I have even posted my complaints in a previous post. My quick and dirty solution has been to simply add the path that contains my DLL to the PATH environment variable and the process would find the .dlls that way. However, if you are using a hosted ASP.NET site or have clients that simply will NOT modify the PATH environment variable, the work around does not work. I have finally found the holy grail of information on how to solve this problem. Why I have not found it before I do not know, and I have search with Google for it a hundred times.

Here is the post I found:

http://blogs.msdn.com/b/jorman/archive/2007/08/31/loading-c-assemblies-in-asp-net.aspx


For ASP.NET, the solution is very simple and elegant. I copy it directly from Jerry Orman's excellent post:

In web.config, add the following…use a path where the Native C++ DLL is located:
<appsettings><add key="NativePath" value="C:\MyNativeDLLs"> </add>

In global.asax, add the following:

protected void Application_Start(object sender, EventArgs e){
String _path = String.Concat(System.Environment.GetEnvironmentVariable("PATH"), ";", ConfigurationSettings.AppSettings["NativePath"]);
System.Environment.SetEnvironmentVariable("PATH", _path, EnvironmentVariableTarget.Process);
}


This post also details other ways of solving the problem as well.