当我运行下面的代码时,我获取句柄无效,它需要一个字符串并将其传递到远程批处理文件

发布于 2024-08-19 04:03:21 字数 1864 浏览 5 评论 0原文

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;
namespace SampleProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            String input = textBox1.Text;

            try
            {

                Process ps = new Process();

                ps.StartInfo.FileName = @"\\199.63.55.163\d$\hello.bat";

                ps.StartInfo.Arguments = input;

                ps.StartInfo.CreateNoWindow = false;

                String domain = ps.StartInfo.Domain;

                ps.StartInfo.RedirectStandardOutput = true;

                ps.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                ps.StartInfo.WorkingDirectory = @"d:\praveen";

                ps.StartInfo.UserName = "Raj";

                ps.StartInfo.Domain = "domain";

                ps.StartInfo.Password = Encrypt("Hello123");


                ps.StartInfo.UseShellExecute = false;

                ps.Start();

                ps.WaitForExit();

                MessageBox.Show(ps.StandardOutput.ReadToEnd());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public static SecureString Encrypt(String pwd)
        {
            SecureString ss = new SecureString();

            for (int i = 0; i < pwd.Length; i++)
            {
                ss.AppendChar(pwd[i]);


            }

            return ss;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Security;
namespace SampleProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            String input = textBox1.Text;

            try
            {

                Process ps = new Process();

                ps.StartInfo.FileName = @"\\199.63.55.163\d$\hello.bat";

                ps.StartInfo.Arguments = input;

                ps.StartInfo.CreateNoWindow = false;

                String domain = ps.StartInfo.Domain;

                ps.StartInfo.RedirectStandardOutput = true;

                ps.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

                ps.StartInfo.WorkingDirectory = @"d:\praveen";

                ps.StartInfo.UserName = "Raj";

                ps.StartInfo.Domain = "domain";

                ps.StartInfo.Password = Encrypt("Hello123");


                ps.StartInfo.UseShellExecute = false;

                ps.Start();

                ps.WaitForExit();

                MessageBox.Show(ps.StandardOutput.ReadToEnd());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public static SecureString Encrypt(String pwd)
        {
            SecureString ss = new SecureString();

            for (int i = 0; i < pwd.Length; i++)
            {
                ss.AppendChar(pwd[i]);


            }

            return ss;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诠释孤独 2024-08-26 04:03:21

这是一个盲目的尝试,但我认为一旦退出,您就无法读取进程的标准输出。

另外,您还必须重定向它 - 请查看此文档: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

It's a shot in the dark, but I think that you can't read the processes standard output once it has exited.

Also you have to redirect it - take a look at this documentation: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

孤独患者 2024-08-26 04:03:21

.NET 进程使用凭据启动进程错误的重复(句柄无效) ?您需要分配 RedirectStandardInput、RedirectStandardOutput、RedirectStandardError

Duplicate of .NET Process Start Process Error using credentials (The handle is invalid) ? You need to assign RedirectStandardInput, RedirectStandardOutput, RedirectStandardError

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文