Develop a File IO ActiveX control in Microsoft .NET

  • Post author:
  • Post category:Blog
  • Post comments:0 Comments
  • Reading time:4 mins read


1. Use this guide to create your own ActiveX control

Writing an ActiveX Control in .NET

2. Design your ActiveX as picture bellow:


3. Use following code behind

namespace FileIOActiveXControl
    public partial class WriteToClientControl : UserControl
        public WriteToClientControl()
        private void btnWrite_Click(object sender, EventArgs e)
            WriteLog(@"c:\temp\log.txt", txtUserInput.Text);
        public void WriteLog(string fileName, string content)
                StreamWriter log = new StreamWriter(fileName, true);
            catch (Exception ex)

3. Create TestActiveX.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
Please note that you MUST load this HTML document from your local IIS, or from a TRUSTED SITE or the control will not load properly on the page due to security policy.
To fix this:<br>
<li>copy the HTM file and the DLL into your c:\inetpub\wwwroot folder (or applicable IIS root directory)
<li>load the document in your browser with the url:  http://localhost/TestActiveX.htm
     <OBJECT id="fileIOActiveX" name="fileIOActiveX" classid="http:FileIOActiveXControl.dll#FileIOActiveXControl.WriteToClientControl" width="450" height="100" VIEWASTEXT>


1. Copy FileIOActiveXControl.dll and TestActiveX.htm to the same folder

2. Go to IIS to create a new web site and point to that folder

3. Use another computer to test your control.

If you control cannot load to page like picture bellow.


a. Try to add the site to Trusted Zone in IE

b. Make sure .dll and .htm in the same folder

If your control is loaded correctly, you will see:


4. Enter some text to the textbox then click Write To File button. You may see:

“Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.”


To fix this. You have to configure Code Access Security for Microsoft .NET on client. Run following command to fix this issue.

“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”


And  (MANDATORY if your client is running OS 64 bit)

“C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\caspol.exe -quiet -machine -chggroup Trusted_Zone FullTrust”


5. Stop IE –> reload the test page –> input some text –> click “Write To File” button again now. There will not have any error message appear. Check the log file from C:\temp\log.txt


Leave a Reply