使 Windows 服务像从特定用户运行一样运行 [英] Make windows service run as if it was run from a specific user

查看:24
本文介绍了使 Windows 服务像从特定用户运行一样运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 Windows 服务来装载和卸载 :

来自 MSDN 的代码:

Process.Start(path + "HelloWorld.exe", args, uname, password, domain);

I want to create a windows service to mount and dismount a True Crypt volume.

(this question is not about true crypt so if you do not know what that program that is ok. True Crypt is just a program that enables you to encrypt data. When you decrypt it then trueCrypt will create a virtual drive where you can see the contents unencrypted).

Here is the code I use in my console application:

static void Main(string[] args)
{
        Test();
}

static void Test()
{
    try
    {
        // location where true crypt is installed
        const string trueCryptExeLocation = @"C:\Program Files\TrueCrypt\TrueCrypt.exe";

        // command line arguments to mount a volume located at 'C:\TC Volumes\vol1.tc' where 
        // it's password is 'demo' and it will be mounted on drive letter y
        const string mountVolumeCmd = @"/v ""C:\TC Volumes\vol1.tc"" /ly /q /p demo /f /a";

        // mount the volume on drive letter y
        Process.Start(trueCryptExeLocation, mountVolumeCmd).WaitForExit();

        // write to the volume!
        System.IO.File.WriteAllText("y:\\someFile.txt", "it works");

        // wait 30 seconds before dismounting
        Thread.Sleep(3000);

        // dismount the volume
        Process.Start(@"C:\Program Files\TrueCrypt\TrueCrypt.exe", @"/ly /q /d /f");
     }
     catch (Exception ex)
     {
        // if there is an error write it to disk
        System.IO.File.WriteAllText(@"A:\Dropbox\Eduardo\WindowsNodeService\WindowsNodeService\bin\Debug\startup.txt", ex.ToString());
     }
}

That code basically mounts a volume writes to it and dismounts it. IF I run that program this is what I see:

  1. When I open the program trueCrypt.exe I can see that the volume is actually mounted.
  2. I can see that the program actually wrote to the file someFile.txt
  3. The path Y:\ exist. In other words I can open My Computer and see drive Y.

Now my question is why if I run the same code on a windows service it runs differently but it runns ok with no errors.

In other words having the following windows service:

public partial class Service1 : ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Test(); // same method as console application
    }

    protected override void OnStop()
    {
    }

   // etc... Test method implementation

Running that code runs fine. It writes to the file someFile.txt! which is great. The problem is that if I open trueCrypt I do not see the volume mounted in there. Also if I go to My Computer I do not see the drive Y. How can I make my windows service behave like the console application?

解决方案

If you're using .Net 4.5, you can use this Process.Start call:

Code from MSDN:

Process.Start(path + "HelloWorld.exe", args, uname, password, domain);

这篇关于使 Windows 服务像从特定用户运行一样运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆