Tuesday, December 23, 2008

Write locked - read shared file

I wanted to open/create a file in one process which locks it for writing but shared for reading which another process can open for reading. It took a while to me. The code is given below:

1) Open a file locked for writing but shared for reading:
FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);

2) In another process/thread, open the file for read (not writing) and readwrite sharing
FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

Friday, November 7, 2008

IPSEC policy update

Click Start, click Run, type in: cmd, click OK
Type: gpupdate /force /sync and press enter.
If they get a permissions error, do the following instead:
Type net stop policyagent and press enter. The policy agent should now stop, this may take a few minutes.
Type net start policyagent and press enter. The policy agent should now start.

Monday, July 7, 2008

Dynamically seting a link to a CSS file in ASP.NET

To dynamically set css file, add runat = server and modify this from the page load:

<link id="MyStyleSheet" rel="stylesheet" type="text/css" runat="server" />

Then in your Page_Load, simply add a "href" attribute as below:

Sub Page_Load(Sender As Object, E As EventArgs)
If Not (IsPostBack)
End If
End Sub

Another way to achive the same is by adding a literal control to the page. The literal contains the link to the css file:

void Page_Load(object sender, EventArgs e)
Literal l = new Literal();
l.Text = "<link rel=\"stylesheet\" href=\"mystyle.css\" type=\"text/css\" />";
l.ID = "mystyle";
if(Page.Header.FindControl(l.ID) == null)

Thursday, June 19, 2008

IIS: HTTP 500 Internal Server Error

HTTP 500 Internal Server Error

If you are facing this IIS error with either of the following events, this post might help you in resolving the issue.

1. The server failed to load application '/LM/W3SVC'. The error was 'The specified metadata was not found.
2. The server failed to load application '/LM/W3SVC/1/Root'. The error was 'Class not registered

a) Make sure Ditributed Transaction Coordinator service is running. If you are facing problem in making it work, this link might help. In brief, start the service using service MMC; run msdtc -resetlog if it fails. Check your application again. If you are fortunate enough, your problem might have resolved. If not, follow next step.

b) Follow the following steps. These steps have been taken from this site.

This error is caused by the component services on the computer. To resolve the error

Click Start menu (Windows) - Settings - Control Panel.

Double-click Administrative Tools.

Double-click Component Services.

Expand the Computers folder, open the My Computer folder, and select COM+ Applications.

If an error occurs when you try to open the My Computer folder, refer to the following Microsoft article: http://support.microsoft.com/?id=301919

Delete the following IIS-related packages contained in the COM+ Applications folder:

IIS In-Process Applications

IIS Out-of-Process Pooled Applications

IIS Utilities

Open Windows command prompt, navigate to %windir%\system32\inetsrv directory and enter the following commands:

c:\> rundll32 wamreg.dll, CreateIISPackage

Note: This command is case-sensitive.

C:\> regsvr32 asptxn.dll


Friday, May 2, 2008

Disabling webpart personalization

The webpart personalization can be disabled using:

1) Set Enabled property of the Webpart manater personalization provider:
WebPartManager1.Personalization.Enabled = false;

2) Disable it while declaring the webpart manager in the .aspx page
<asp:webpartmanager runat="server" id="wpmgr1"
personalization-enabled="false" />

Webpart personalization infrastructure

Read the following post which is mostly taken from MSDN, if you are having problem due to ASP.NET personalization provider. This might be required when you are trying to dynamically add/delete webparts and the inconsistent behaviour is observed; specifically when user logs in.

WebPartPersonalization implements low level personalization infrastructure.

This class implements the logic required to carry out lower-level personalization operations. Although the WebPartManager class manages the high-level life cycle of personalization, it is the WebPartPersonalization class that is responsible for actually implementing the physical steps necessary to carry out specific personalization actions. The WebPartPersonalization class in turn relies on an implementation of PersonalizationProvider to communicate with the underlying data store for personalization information.

The WebPartPersonalization class works closely with a WebPartManager control and the rest of the personalization infrastructure.

If you want to create a personalization subsystem significantly different from that provided by the WebPartPersonalization and the WebPartManager classes, you should create a custom WebPartPersonalization implementation by deriving from WebPartPersonalization and add your own custom logic. Then create a custom WebPartManager implementation by deriving from WebPartManager, add your own custom logic, and override the CreatePersonalization method to return your custom WebPartPersonalization implementation. Because a WebPartManager control makes requests to the personalization infrastructure by way of a WebPartPersonalization instance, the WebPartManager control does not directly interact with or hold references to PersonalizationProvider implementations.

Wednesday, April 16, 2008

Reseting user personalization state

To manage personalization state of users, you must use the PersonalizationAdministration class. The following example shows how te reset state of the current user:

WebPartManager mgr = WebPartManager.GetCurrentWebPartManager(Page);

To reset state of a particular user, use the following code snippet:

For managing user state, refer to http://msdn2.microsoft.com/en-us/library/ms366517.aspx

// Reset the user.
if (! PersonalizationAdministration.ResetUserState("~/Default.aspx", txtUser.Text))
Response.Write("The user could not be found or the user has not personalized this page");