I have a program that reads stuff from a Searial COM port. It uses one of those USB to COM port gizmos (Prolific driver).
The environment is Win7/Server 2008R2 (all 64 bit), joined to domain, .NET4.
The program works fine when run from within context of an end user.
However, when i try to run the same program as a windows service I get 'The system cannot find the file specified'. I tried to configure the service to run both as a local system account and as a domain service account (added to the admin ug), but still the same error.
I also created a little test program (see the code at the end of the post, it is stolen from somewhere on the internet).
This runs fine when run from the console and opens the port and reports its type (CHAR), but fails with the same error (File not found) when i try to run it as a scheduled job.
Again i tried running it both under my own account and under account of a service (both added as local admins) and it is the same error.
I am pretty sure it is some sort of a security/policy issue but cannot quite figure out what.
Any pointers will be greatly appreciated,
static void Main(string args)
var portName = @"\\.\COM3";
var handle = CreateFile(portName, 0, 0, IntPtr.Zero, 3, 0x80, IntPtr.Zero);
if (handle == (IntPtr)(-1))
Console.WriteLine("Could not open " + portName + ": "
+ new Win32Exception().Message);
var type = GetFileType(handle);
Console.WriteLine("File " + portName + " reports its type as: " + type);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall,
SetLastError = true)]
public static extern IntPtr CreateFile(
static extern FileType GetFileType(IntPtr hFile);
enum FileType : uint
UNKNOWN = 0x0000,
DISK = 0x0001,
CHAR = 0x0002,
PIPE = 0x0003,
REMOTE = 0x8000,