title: Using BITS (Background Intelligent Transfer Service) from C#
body: <p>I'd like to write some code that uses BITS for copying very large files between disks on the same server (the second disk being a SAN level clone\snapshot).</p>
  
  <p>I looked into BITS as i thought it would be a good way to get progress\percentage complete data on the transfers, as well as resume functionality etc</p>
  
  <p>I have a lot of hosted Powershell and I thought i'd have a stab at using the inbuilt BITS cmdlets, as this would be a super quick way of doing it, i could write wrappers to get the stuff i need etc Unfortunately i ran into  this: </p>
  
  <blockquote>
    <p>When you use *-BitsTransfer cmdlets from within a process that runs in a noninteractive context, such as a Windows service, you may not be able to add files to BITS jobs, which can result in a suspended state. For the job to proceed, the identity that was used to create a transfer job must be logged on. For example, when creating a BITS job in a PowerShell script that was executed as a Task Scheduler job, the BITS transfer will never complete unless the Task Scheduler's task setting "Run only when user is logged on" is enabled."</p>
  </blockquote>
  
  <p>Doing anything through an impersonated Powershell runspace throws up the following error:</p>
  
  <blockquote>
    <p>The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist. (Exception from HRESULT: 0x800704DD)"</p>
  </blockquote>
  
  <p>My web service is running as AppPoolIdentity, I impersonate when needed to do stuff. It makes sense this doesn't work via hosted Powershell, but can anyone think of a workaround? If this isn't possible, do i have any alternatives? </p>
  
  <p>I was thinking of using BITS Compact Server as an alternative, but the documentation is ancient.</p>