在使用C#构建的WinForms应用程序中运行.JS代码 [英] Run .JS Code in WinForms App Built with C#

查看:54
本文介绍了在使用C#构建的WinForms应用程序中运行.JS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


如果这不是这个问题的正确位置,必须道歉。


我使用了很多脚本在我的组织中设置计算机时的注册文件,我正在构建一个将这些脚本包含在按钮中的工具,这样我就可以运行该工具,点击按钮,让神奇的事情发生。


其中一个脚本是一个.js文件,用于将机器添加到在线系统,执行此文件的常用方法是右键单击它并使用命令提示符运行它,文件运行,然后显示"更新完成"消息在
结束。


我正在尝试将该文件包含在我的应用程序中,它将像往常一样在命令提示符下运行,但只需单击一个按钮,这里的问题是,我希望我的应用程序是一个单独的.exe独立应用程序,没有其他文件(为了方便访问和
使用),所以我一直在考虑将js代码包含在C#中的按钮中,但我无法找到一个包含和执行代码的正确方法,而无需调用实际的.js文件,我还需要显示命令提示符窗口,以便它指示
脚本正在运行因为脚本本身没有表明它在"更新完成"之前正在运行。如果它运行了多次,它将向在线系统添加一个重复值。


是否有办法在命令提示符下执行文件,而不必包含副本该文件与.exe一起?将它添加为组件或许?在显示脚本正在运行的命令提示符窗口(或其他指示符)的同时?



感谢帮助,如果这不是再次抱歉这个帖子的正确位置。

解决方案

将文件放在项目资源中,解压缩,使用。在表单关闭事件中,如果文件在那里删除它。没有保证,例如计算机上的电源关闭然后文件将不会被删除。

命名空间WindowsFormsApp1 
{

公共部分类Form1 :Form
{
private readonly string _fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory," SomeFile.js");

public Form1()
{
InitializeComponent();
FormClosing + = Form1_FormClosing;
}

private void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
if(File.Exists(_ fileName))
{
File.Delete(_filename);
}
}

private void button1_Click(object sender,EventArgs e)
{
File.WriteAllText(_fileName,Properties.Resources.MyJavaScriptFile);
//对文件做一些事情。
}
}
}



Hello!

Obligatory apology if this isn't the right place for this question.

I use a lot of scripts and registery files when setting up computers in my organization, and i'm working on building a tool that would include those scripts into buttons, so i can run the tool, click the buttons and let the magic happen.

One of those scripts is a .js file that works on adding machines to an online system, the usual way i execute this file is by right clicking it and running it with Command Prompt, the file runs, then displays an "update complete" message at the end.

I'm trying to include that file in my application where it would run in command prompt as usual, but at the click of a button, the issue here is that i want my application to be a single .exe standalone app, with no additional files (for easy access and use), so i've been thinking to include the js code inside a button in C#, but i couldn't find a proper way to include and execute the code without having to call the actual .js file, and i also need to have the command prompt window appear so it gives an indication that the script is running, as the script itself gives no indication that it's running before the "update complete" message, and it will add a duplicate value to the online system if it was run more than once.

Is there a way to have the file executed in command prompt without having to include the copy the file along with the .exe? add it as a component perhaps? while having the command prompt window (or another indicator) appear that the script is running?

Appreciate the help, and sorry again if this isn't the right place for this post.

解决方案

Place the file in project resource, extract, use. In form closing event if the file is there delete it. There are not guarantees e.g. power is turn off on the computer then the file would not get removed.

namespace WindowsFormsApp1
{
    
    public partial class Form1 : Form
    {
        private readonly string _fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SomeFile.js");

        public Form1()
        {
            InitializeComponent();
            FormClosing += Form1_FormClosing;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (File.Exists(_fileName))
            {
                File.Delete(_fileName);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.WriteAllText(_fileName, Properties.Resources.MyJavaScriptFile);
            // do something with the file.
        }
    }
}


这篇关于在使用C#构建的WinForms应用程序中运行.JS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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