0

我有一个在登录名“UserA”下运行的 Windows 服务。Windows 服务的工作是在计时器经过的事件上启动一个新进程。

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
    Task.Factory.StartNew(() => StartNewProcess());
}

private void Initialize()
{
    newProcess = new Process();
    newProcess.StartInfo.FileName = "Test.exe";
    newProcess.StartInfo.Arguments = "sessionId...";
    newProcess.StartInfo.CreateNoWindow = false;
    newProcess.StartInfo.ErrorDialog = false;
    newProcess.StartInfo.RedirectStandardError = true;
    newProcess.StartInfo.RedirectStandardInput = true;
    newProcess.StartInfo.UseShellExecute = false;
    newProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;            
}

private void StartNewProcess()
{
    newProcess.Start();
}

在任务管理器上,我看到 Windows 服务和新进程都有一个“用户名”作为“UserA”。

但问题是 Windows 服务能够访问“C:\QueryResult”,但新进程无法访问“C:\QueryResult”

File.Copy("C:\QueryResult", "D:\blahblah")在这两个过程中都使用

新流程中的安全上下文是否发生了变化?

4

1 回答 1

0

尝试像这样自动提升应用程序权限:

using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Windows.Forms;

public static class Program
{
    [STAThread]
    public static void Main()
    {
        if (!IsAdmin() && IsWindowsVistaOrHigher())
            RestartElevated();
        else
            DoStuff();
    }

    private static Boolean IsAdmin()
    {
        WindowsIdentity identity = WindowsIdentity.GetCurrent();

        if (identity != null)
            return (new WindowsPrincipal(identity)).IsInRole(WindowsBuiltInRole.Administrator);

        return false;
    }

    private static Boolean IsWindowsVistaOrHigher()
    {
        OperatingSystem os = Environment.OSVersion;
        return ((os.Platform == PlatformID.Win32NT) && (os.Version.Major >= 6));
    }

    private static void RestartElevated()
    {
        String[] argumentsArray = Environment.GetCommandLineArgs();
        String argumentsLine = String.Empty;

        for (Int32 i = 1; i < argumentsArray.Length; ++i)
            argumentsLine += "\"" + argumentsArray[i] + "\" ";

        ProcessStartInfo info = new ProcessStartInfo();

        info.Arguments = argumentsLine.TrimEnd();
        info.CreateNoWindow = false;
        info.ErrorDialog = false;
        info.FileName = Application.ExecutablePath;
        info.RedirectStandardError = true;
        info.RedirectStandardInput = true;
        info.UseShellExecute = false;
        info.Verb = "runas";
        info.WindowStyle = ProcessWindowStyle.Hidden; 
        info.WorkingDirectory = Environment.CurrentDirectory;

        try
        {
            Process.Start(info);
        }
        catch { return; }

        Application.Exit();
    }
}
于 2013-01-15T17:15:33.323 回答