Skip to content

Instantly share code, notes, and snippets.

@kphillisjr
Created August 22, 2018 23:47
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kphillisjr/a5d668d9e4591e1b1a8b95c57223cf55 to your computer and use it in GitHub Desktop.
Save kphillisjr/a5d668d9e4591e1b1a8b95c57223cf55 to your computer and use it in GitHub Desktop.
JSCript.NET Working Examples
/*
To Compile:
PATH\\TO\\jsc.exe QueryDotnet.js
This example is based on:
https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
*/
import System;
import Microsoft.Win32;
/* The Following is for Checking for .NET version 1 to 4.0 */
function GetVersionFromRegistry(): void
{
// Opens the registry key for the .NET Framework entry.
var ndpKey :RegistryKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\", false);
/*
// As an alternative, if you know the computers you will query are running .NET Framework 4.5
// or later, you can use:
var ndpKey :RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).
OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\", false);
*/
for (var versionKeyIndex in ndpKey.GetSubKeyNames())
{
var versionKeyName = ndpKey.GetSubKeyNames()[versionKeyIndex];
if (versionKeyName.StartsWith("v"))
{
var versionKey:RegistryKey = ndpKey.OpenSubKey(versionKeyName);
var name:String = versionKey.GetValue("Version", "");
var sp:String = versionKey.GetValue("SP", "").ToString();
var install:String = versionKey.GetValue("Install", "").ToString();
if (install == "") {
//no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
} else {
if (sp != "" && install == "1")
{
Console.WriteLine(versionKeyName + " " + name + " SP" + sp);
}
}
if (name != "")
{
continue;
}
for (var subKeyIndex in versionKey.GetSubKeyNames())
{
var subKeyName = versionKey.GetSubKeyNames()[subKeyIndex]
var subKey:RegistryKey = versionKey.OpenSubKey(subKeyName);
name = subKey.GetValue("Version", "");
if (name != "")
sp = subKey.GetValue("SP", "").ToString();
install = subKey.GetValue("Install", "").ToString();
if (install == ""){
//no install info, must be later.
Console.WriteLine(versionKeyName + " " + name);
} else {
if (sp != "" && install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name + " SP" + sp);
}
else if (install == "1")
{
Console.WriteLine(" " + subKeyName + " " + name);
}
}
}
}
}
}
GetVersionFromRegistry();
/* The Following Section is to Query for .NET version 4.5 or newer */
function Get45PlusFromRegistry() :void
{
var subkey:String = "SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full\\";
var myHKLM :RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var ndpKey :RegistryKey = myHKLM.OpenSubKey(subkey,false);
if (ndpKey != null && ndpKey.GetValue("Release") != null) {
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion(ndpKey.GetValue("Release")));
} else {
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
// Checking the version using >= will enable forward compatibility.
function CheckFor45PlusVersion(releaseKey:int) : String
{
if (releaseKey >= 461808)
return "4.7.2 or later";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
Get45PlusFromRegistry()
/*
To Compile:
PATH\\TO\\jsc.exe QuoteOfTheDay.js
Example this Code is based on:
https://github.com/dotnet/core/blob/master/samples/qotd/Program.cs
*/
import System;
import System.IO;
function Main(args : String[]) : void
{
if (args.length <=1)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You must specify a quotes file.");
Console.ResetColor();
return;
}
var quotes = File.ReadAllLines(args[1]);
var randomQuote:String = quotes[new Random().Next(0, quotes.Length-1)];
Console.WriteLine("[QOTD]: {0}", randomQuote);
};
// Run the Program:
var myArguments : String[] = Environment.GetCommandLineArgs();
Main(myArguments);
/*
To Compile with Console Window Visible use:
PATH\\TO\\jsc.exe SampleForm.js
To Compile without a Console Window use:
PATH\\TO\\jsc.exe /t:winexe SampleForm.js
Example this Code is based on:
https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form?view=netframework-4.7.2
*/
import System;
// For Point();
import System.Drawing;
import System.EventArgs;
/*
NOTE: Point is also Defined in System.Windows, but JScript .NET requires the
Assembly information from System.Drawing.
*/
import System.Windows;
import System.Windows.Forms;
import Accessibility; // Required.
// Add a Button Click Event handler
class MyForm {
// This is the OnClick Handler for First Button.
private function myClickCancelButton(sender:Object, e:System.EventArgs) : void {
Console.WriteLine("myClickCancelButton ( " + sender.Text + " )");
}
/* Custom Button Class with Event Handler */
private class myButton extends Button
{
/* Alternate way to Register a Button Click */
protected function OnClick (e:System.EventArgs):void{
Console.WriteLine("myButton:OnClick ( " + this.Text + " )");
super.OnClick (e);
}
}
function CreateMyForm()
{
// Create a new instance of the form.
var form1 : Form = new Form();
// Create two buttons to use as the accept and cancel buttons.
var button1 : Button = new myButton ();
var button2 : Button = new Button ();
// Set the text of button1 to "OK".
button1.Text = "OK";
// Set the position of the button on the form.
button1.Location = new Point (10, 10);
// Set the text of button2 to "Cancel".
button2.Text = "Cancel";
// Set the position of the button based on the location of button1.
button2.Location
= new Point (button1.Left, button1.Height + button1.Top + 10);
// Add a event handler:
button2.add_Click(myClickCancelButton);
// Set the caption bar text of the form.
form1.Text = "My Dialog Box";
// Display a help button on the form.
form1.HelpButton = true;
// Define the border style of the form to a dialog box.
form1.FormBorderStyle = FormBorderStyle.FixedDialog;
// Set the MaximizeBox to false to remove the maximize box.
form1.MaximizeBox = false;
// Set the MinimizeBox to false to remove the minimize box.
form1.MinimizeBox = false;
// Set the accept button of the form to button1.
form1.AcceptButton = button1;
// Set the cancel button of the form to button2.
form1.CancelButton = button2;
// Set the start position of the form to the center of the screen.
form1.StartPosition = FormStartPosition.CenterScreen;
// Add button1 to the form.
form1.Controls.Add(button1);
// Add button2 to the form.
form1.Controls.Add(button2);
// Display the form as a modal dialog box.
form1.ShowDialog();
}
}
var myFormA = new MyForm();
// Create the Dialog:
myFormA.CreateMyForm();
/*
compile using:
PATH\\TO\\jsc.exe ThreadInfo.js
This example is based on the example on:
https://docs.microsoft.com/en-us/dotnet/api/system.threading.thread?view=netframework-4.7.2
*/
import System;
import System.Threading;
class myTask
{
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static function ThreadProc():void {
for (var i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
static public function Main():void
{
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
var tProc:ThreadStart = ThreadProc;
var t = new Thread(tProc);
// This should work, but does not.
//var t = new Thread(ThreadProc);
// Start ThreadProc. Note that on a uniprocessor, the new
// thread does not get any processor time until the main thread
// is preempted or yields. Uncomment the Thread.Sleep that
// follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);
for (var i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
// Run the main function.
myTask.Main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment