Sandip's Programming Zen

An attempt to share tech/coding experiences

Archive for the ‘Programming’ Category

Resolved: “Access denied” error on System.Diagnostics.Process.Start

with 3 comments

Wrote a piece of code which would launch an application exe using Process.Start, on development machine it worked without a problem but when deployed on client machine it started giving “Access Denied” error.

It turned out that when the code runs in domain controlled user environement Process.Start needs a set of security credentials by default to run. Now, in practical scenario we can not just hard code any credentials or even prompt for input.

The solution, there is an attribute called UseShellExecute which needs to be set False , this will allow the exe to be launched without credentials.

Sample code goes like this :

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = _path;
p.StartInfo.Arguments = string.Format(“\”{0}\” \”{1}\””, arg1, arg2);
p.StartInfo.UseShellExecute = false;


Written by Sandip

August 6, 2011 at 5:03 pm

Posted in .Net, Programming, Softwares

Running 32 bit applications on 64 bit OS

with one comment

Lately I ran into a problem where I created a feature where user can view PDF within a winform application. To do that I first used a web browser control since it was a simple solution where you just need to provide pdf path to the navigate property. That works without problem on Win XP (32bit) , but as soon as user runs it on his Vista (64 bit) OS  , it behaved different , that said, pdf opens but in a separate window, which looks bit odd, so must be fixed.

To solve that problem , I have removed web browser control and placed Acrobat Reader control (assuming that all of my users will have at least Adobe reader installed). Now that again worked well on Win XP but simply crashed on Vista (64 bit).  Reason was Acrobat reader control is an ActiveX control which is 32 bit component , that won’t work on a 64 bit OS. After looking around for some time and find out there is no 64 bit version of Acrobat reader, I discovered that I have to make sure that my win form app runs under 32 bit process on a 64 bit OS. To, do that you have to change Target Platform property of your project (See screenshot below).

Bottom line is if you have any project dependencies which only works on 32 bit environment, you have to change your project’s target platform to run only under 32 bit process. That will make your app work fine on both 32 bit and 64 bit OSes.


Written by Sandip

January 9, 2011 at 10:44 pm

Posted in .Net, Programming, Windows

Tagged with , , ,

C#: Accessing resources on network

leave a comment »

Accessing files and other resources on network from within code is achieved in following way in C# (of course there are other ways but this seems neat):

Take following assembly reference first:

using System.Runtime.InteropServices;

[DllImport(“advapi32.dll”, SetLastError = true, CharSet = CharSet.Unicode)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

Private void YourFunction()


WindowsImpersonationContext wic;
IntPtr tokenHandle = new IntPtr(0);
bool returnValue = LogonUser(f.cUserName, f.cDomain, f.cPassword, 2, 0, ref tokenHandle);
WindowsIdentity ImpersonatedIdentity = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext MyImpersonation = ImpersonatedIdentity.Impersonate();

wic = MyImpersonation;

// do some file IO activity here on server…



Written by Sandip

March 15, 2010 at 1:16 pm

Posted in .Net, Programming

Tagged with , ,

C# MP3 files organizer

leave a comment »

I have a huge collection of mp3 music files and locating music was bit difficult if I would like to hear a particular artist, I quickly tried to search on google , found some programs but unnecessarily complex, so I wrote my own , which is very simple.  it does following:

  • Asks for a target directory where your mp3 files are located.
  • On click of a button it extracts the meta info and organizes the files in folders per artist.

It’s very basic but useful to me at least. you are free to modify/use/distribute at your own risk 🙂

Download it from here.

Written by Sandip

March 15, 2010 at 1:07 pm

Posted in .Net, Programming

Tagged with ,

Retrieve compilation date of a .Net Assembly

leave a comment »

I wanted to display last update date on the application interface so users know when something is changed, mostly at the time of customer acceptance tests. I found following code from here. cool.

private DateTime DateCompiled()
    // The assembly version must have the last two numbers removed and replaced with *
    // It should look something like this: 
    // [assembly: AssemblyVersion("1.0.*")]
    //Build dates start from 01/01/2000
    System.DateTime result = DateTime.Parse("1/1/2000");
    //Retrieve the version information from the assembly from which this code is being executed
    System.Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
    //Add the number of days (build)
    result = result.AddDays(version.Build);
    //Add the number of seconds since midnight (revision) multiplied by 2
    result = result.AddSeconds(version.Revision * 2);
    //If we're currently in daylight saving time add an extra hour
    if (TimeZone.IsDaylightSavingTime(System.DateTime.Now, 
        result = result.AddHours(1); 
    return result;

Written by Sandip

February 16, 2010 at 11:11 am

Posted in .Net, Asp.Net, Programming

Tagged with ,

Replacing “src” property using Javascript in embed tag

leave a comment »


If you try to replace the src property of embed tag using javascript , it will work fine in Firefox but not in IE.

To make it work in both you have to  wrap the embed tag in <div> and then replace the whole embed tag in div every time you want a change in src propert.

Written by Sandip

January 7, 2010 at 12:28 pm

C# Winforms: Sending an email with attachment

leave a comment »

Sending an email from C# winforms application is exactly same as we do in webapps using System.Net.Mail namespace. Here is a sample code which sends out an email with an attachment (again it is not specific to winforms can be used in webapps as it is).

MailMessage msg = new MailMessage();
SmtpClient client;
Attachment attach = new Attachment(filepath);

if (varEmailID.IndexOf(',') > 0)
    string[] iDs;
    iDs = varEmailID.Split(',');
    for (int i = 0; i <= iDs.Length - 1; i++)
        msg.To.Add(new MailAddress(iDs.GetValue(i).ToString()));
    msg.To.Add(new MailAddress(varEmailID));

msg.Subject = varSubject;
msg.Body = varBody;
msg.IsBodyHtml = false;
msg.Priority = MailPriority.Normal;

string FromEmail = ConfigurationSettings.AppSettings["SystemMailID"];
string SmtpServer = ConfigurationSettings.AppSettings["SmtpServer"];
string UserName = ConfigurationSettings.AppSettings["UserName"];
string Password = ConfigurationSettings.AppSettings["Password"]; 
client = new SmtpClient(SmtpServer);
client.UseDefaultCredentials = false;
System.Net.NetworkCredential creds = new System.Net.NetworkCredential(UserName, Password);
client.Credentials = creds;

msg.From = new MailAddress(FromEmail);


Written by Sandip

October 17, 2009 at 4:22 am

Posted in .Net, Programming

Tagged with ,