执行进程不起作用

本文关键字:不起作用 进程 执行 | 更新日期: 2023-09-27 18:35:42

情况:

我想通过执行目录配额的进程修改文件服务器上的文件夹配额.exe

问题:

正在执行的进程根本不给出任何结果

迄今:

我已经重定向了在我的文件服务器上执行的进程和参数,以仔细查看服务器端到底发生了什么。

执行的过程没有例外,一切似乎都很好。 当我的文件服务器上查看当前文件夹配额时,没有任何变化。
我决定将我的参数复制粘贴到服务器上的 CMD.exe 中,然后一切都很好......
我不明白为什么它在我的文件服务器上不起作用,可能很简单,但我在这里需要一些帮助

重要信息:

我正在我的文件服务器上安装Windows服务并通过SOUPUI调用该方法(这一切都工作正常)。

已安装的服务以域管理员身份运行,并具有执行这些操作所需的所有权限

班级

public class Quota
    {
        public string FolderLocation;
        public int SizeInMB;
        public string FileServerName;
    }

方法

public string SetFolderQuota(Quota quota)
        {
            Process QuotaProcess = new Process();
            QuotaProcess.StartInfo.RedirectStandardOutput = false;            
            QuotaProcess.StartInfo.FileName = @"cmd.exe";
            QuotaProcess.StartInfo.UseShellExecute = true;
            QuotaProcess.StartInfo.Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
            try
            {                
                QuotaProcess.Start();
            }
            catch(Exception Ex)
            {
                return Ex.Message;
            }
            return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
        }

执行进程不起作用

发现了问题

目录配额.exe使用 Windows-on Windows 64 位重定向进行重定向。正在发生的事情是我的启动请求(来自 32 位进程)被重定向到 %windir%''SysWOW64''dirquota.exe。由于 64 位安装上没有此特定可执行文件的 32 位版本,因此启动失败。要绕过此过程并允许我的 32 位进程访问本机(64 位)路径,我必须引用 %windir%''sysnative

《守则》

 public string SetFolderQuota(Quota quota)
        {
            string FileLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows),@"sysnative'dirquota.exe");
            Process QuotaProcess = new Process();
            QuotaProcess.StartInfo.RedirectStandardOutput = false;
            QuotaProcess.StartInfo.FileName = FileLocation;
            QuotaProcess.StartInfo.UseShellExecute = true;
            QuotaProcess.StartInfo.Arguments = " Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName;
            try
            {                
                QuotaProcess.Start();
            }
            catch(Exception Ex)
            {
                return Ex.Message + Environment.NewLine + "FileLocation: " + FileLocation;
            }
            return "Correctly Executed: " + QuotaProcess.StartInfo.FileName + QuotaProcess.StartInfo.Arguments;
        }
最好是

您可以将 Process 的输出重定向到日志文件并查看实际异常是什么。

ProcessStartInfo process = new ProcessStartInfo
{ 
   CreateNoWindow = false,
   UseShellExecute = false, 
   RedirectStandardOutput = true,
   FileName = @"cmd.exe",
   Arguments = "/C " + "dirquota Quota Add /PATH:" + '"' + quota.FolderLocation + '"' + " /Limit:" + quota.SizeInMB + "mb" + " /remote:" + quota.FileServerName
};
Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();

记录输出值以获取由执行此命令引起的确切异常