Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为Neo4j服务经理编程?_C#_Powershell_Neo4j - Fatal编程技术网

C# 如何为Neo4j服务经理编程?

C# 如何为Neo4j服务经理编程?,c#,powershell,neo4j,C#,Powershell,Neo4j,我正在尝试用C#编写一个Neo4j service manager程序,它可以使用当前的Neo4j版本3.3.5启动和停止本地图形数据库。该程序应复制已停止运行的Neo4j ce.exe程序的功能。程序将调用与Neo4j.bat文件中相同的PowerShell命令。这是我尝试编写的第一个PowerShell脚本 到目前为止,在启动时,程序会发出许多异常错误,以“找不到或打开PDB文件”结尾。当我运行PowerShell实例代码时,会引发安全异常 Exception thrown: 'System

我正在尝试用C#编写一个Neo4j service manager程序,它可以使用当前的Neo4j版本3.3.5启动和停止本地图形数据库。该程序应复制已停止运行的
Neo4j ce.exe
程序的功能。程序将调用与
Neo4j.bat
文件中相同的PowerShell命令。这是我尝试编写的第一个PowerShell脚本

到目前为止,在启动时,程序会发出许多异常错误,以“找不到或打开PDB文件”结尾。当我运行PowerShell实例代码时,会引发安全异常

Exception thrown: 'System.DllNotFoundException' in System.Management.Automation.dll
Exception thrown: 'System.Security.SecurityException' in mscorlib.dll
。。。然而,
PowerShellInstance.Streams.Error.Count
是零

Neo4j.bat
包含对PowerShell的调用:

PowerShell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command "try { Unblock-File -Path '%~dp0Neo4j-Management\*.*' -ErrorAction 'SilentlyContinue' } catch {};Import-Module '%~dp0Neo4j-Management.psd1'; Exit (Invoke-Neo4j %*)"
Neo4jServiceManager.exe
包含此按钮事件:

private void btnNeo4jStart_Click(object sender, EventArgs e)
{
    // Create runspace to be used by PowerShell instance.
    Runspace myRunspace = RunspaceFactory.CreateRunspace();
    myRunspace.Open();

    // Start PowerShell instance.
    using (PowerShell PowerShellInstance = PowerShell.Create())
    {
        // Add runspace to PowerShell instance.
        PowerShellInstance.Runspace = myRunspace;

        // Add command to pipeline.
        // Import module: Neo4j-Management.psd1 and execute Invoke-Neo4j cmdlet.
        PowerShellInstance.AddScript(@"C:\; cd C:\neo4j-community-3.3.5; SETLOCAL; try { Unblock-File -Path 'C:\neo4j-community-3.3.5\bin\Neo4j-Management\*.*' -ErrorAction 'SilentlyContinue' } catch {}; Exit ('C:\neo4j-community-3.3.5\bin\Neo4j-Management\Invoke-Neo4j.psd1 install-service')");

        // Invoke execution on the pipeline (and collect output).
        Collection<PSObject> PSOutput = PowerShellInstance.Invoke();

        // Loop through each output object item.
        foreach (PSObject outputItem in PSOutput)
        {
            // if null object was dumped to the pipeline during the script then a null
            // object may be present here. check for null to prevent potential NRE.
            if (outputItem != null)
            {
                //TODO: do something with the output item 
                // outputItem.BaseOBject
            }
        }

        // Check the other output streams (for example, the error stream).
        // If PowerShellInstance.Streams.Error.Count = 1, then
        // PowerShellInstance.Streams.Error[0].CategoryInfo returns information.
        if (PowerShellInstance.Streams.Error.Count > 0)
        {
            // error records were written to the error stream.
            // do something with the items found.
        }
    }
}
private void btnNeo4jStart_单击(对象发送方,事件参数e)
{
//创建PowerShell实例要使用的运行空间。
运行空间myRunspace=RunspaceFactory.CreateRunspace();
myRunspace.Open();
//启动PowerShell实例。
使用(PowerShell PowerShellInstance=PowerShell.Create())
{
//将运行空间添加到PowerShell实例。
PowerShellInstance.Runspace=myRunspace;
//将命令添加到管道。
//导入模块:Neo4j-Management.psd1并执行Invoke-Neo4j cmdlet。
PowerShellInstance.AddScript(@“C:\;cd C:\neo4j-community-3.3.5;SETLOCAL;尝试{解除阻止文件-路径'C:\neo4j-community-3.3.5\bin\neo4j管理\*.'-ErrorAction'SilentlyContinue'}捕获{};退出('C:\neo4j-community-3.3.5\bin\neo4j管理\调用-neo4j.psd1安装服务');
//在管道上调用执行(并收集输出)。
集合PSOutput=PowerShellInstance.Invoke();
//循环遍历每个输出对象项。
foreach(PSOutput中的PSObject输出项)
{
//如果在脚本期间将null对象转储到管道中,则为null
//对象可能存在于此处。请检查null以防止潜在的NRE。
if(outputItem!=null)
{
//TODO:对输出项执行某些操作
//outputiItem.BaseOBject
}
}
//检查其他输出流(例如,错误流)。
//如果PowerShellInstance.Streams.Error.Count=1,则
//PowerShellInstance.Streams.Error[0]。CategoryInfo返回信息。
如果(PowerShellInstance.Streams.Error.Count>0)
{
//错误记录已写入错误流。
//对找到的物品做些什么。
}
}
}

您需要查看在PowerShell中发出的命令(例如:
setlocal
不是PowerShell命令,而是
cmd.exe
命令)。另外,
%~dp
语法也是
cmd.exe
。关键是PowerShell不是一个
cmd.exe
drop-in替代品;概念和语法是不同的。我怎么知道EXE和BAT文件名应该被视为“代码”并突出显示代码?我找不到堆栈溢出样式指南。这里有很多关于如何格式化信息的内容,但没有关于格式化规则的内容。我之前的评论与你写的内容无关,我应该说。我试图回复那些对我原来的帖子进行格式更改的编辑们。我找不到写信给编辑,感谢他们,或者得到建议的方法。所以我做了一个评论,希望编辑们能看到。正如你指出的,SETLOCAL对PowerShell没有任何意义,所以我从脚本中删除了它。如果您查看我的原始帖子,您会发现我在PowerShell脚本中将批处理文件中的扩展名替换为完整路径名。(“CD”似乎在PowerShell中工作。)我将尝试编写一系列在PowerShell提示符下工作的命令,然后将它们放入脚本中。如果我成功了,我会把答案贴出来。感谢您尝试帮助我。
cd
是PowerShell中设置位置的别名。该命令可用,但其工作方式与
cmd.exe
cd
命令不同。我建议在深入研究PowerShell之前先学习PowerShell;这可能会帮你省去很多挫折。