且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在使用C#构建的WinForms应用程序中运行.JS代码

更新时间:2023-10-11 20:45:10

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

命名空间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.
        }
    }
}