In this Article, we will discuss about creating Angular project.
FormMain.cs
--------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AngularProjectWizard
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
// Defaults
txtNodePath.Text = GuessNodePath();
txtAngularVersion.Text = "latest";
chkStandalone.Checked = true;
chkDefaults.Checked = true;
chkBuildAfterCreate.Checked = true;
txtAngularCliBase.Text = "npx --package @angular/cli";
}
private static string GuessNodePath()
{
string[] candidates = new[]
{
@"C:\Program Files\nodejs\node.exe",
@"C:\Program Files (x86)\nodejs\node.exe"
};
foreach (var c in candidates)
{
if (File.Exists(c)) return c;
}
try
{
var p = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "where",
Arguments = "node",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
var line = p.StandardOutput.ReadLine();
p.WaitForExit(1500);
if (!string.IsNullOrWhiteSpace(line) && File.Exists(line))
return line;
}
catch { }
return string.Empty;
}
private void btnBrowseLocation_Click(object sender, EventArgs e)
{
using var fbd = new FolderBrowserDialog { Description = "Choose the folder that will contain the new Angular app" };
if (fbd.ShowDialog() == DialogResult.OK)
{
txtLocation.Text = fbd.SelectedPath;
if (string.IsNullOrWhiteSpace(txtProjectName.Text)) txtProjectName.Text = "AngularApp";
}
}
private void btnBrowseNode_Click(object sender, EventArgs e)
{
using var ofd = new OpenFileDialog { Filter = "node.exe|node.exe|All files|*.*", Title = "Select Node interpreter (node.exe)" };
if (ofd.ShowDialog() == DialogResult.OK) txtNodePath.Text = ofd.FileName;
}
private async void btnCreate_Click(object sender, EventArgs e)
{
var nodePath = txtNodePath.Text.Trim();
var baseCli = txtAngularCliBase.Text.Trim();
var version = txtAngularVersion.Text.Trim();
var location = txtLocation.Text.Trim();
var name = txtProjectName.Text.Trim();
if (string.IsNullOrWhiteSpace(location) || !Directory.Exists(location))
{
MessageBox.Show("Please choose a valid target folder.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrWhiteSpace(name))
{
MessageBox.Show("Please enter a project name.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string fileName = "npx";
if (!string.IsNullOrWhiteSpace(nodePath) && File.Exists(nodePath))
{
var npxFromNode = nodePath.Replace("node.exe", "npx.cmd");
if (File.Exists(npxFromNode)) fileName = $"\"{npxFromNode}\"";
}
var cliPkg = string.IsNullOrWhiteSpace(version) ? "@angular/cli" : $"@angular/cli@{version}";
var args = $"--yes --package {cliPkg} ng new {name} --no-interactive";
if (chkDefaults.Checked) args += " --defaults";
if (chkStandalone.Checked) args += " --standalone=true";
ToggleUi(false);
txtOutput.Clear();
WriteLine(">>> Working directory: " + location);
WriteLine($">>> Running: {fileName} {args}");
progress.Style = ProgressBarStyle.Marquee;
try
{
var exit = await RunProcessAsync(fileName, args, location, (line) => WriteLine(line));
progress.Style = ProgressBarStyle.Blocks;
if (exit == 0)
{
WriteLine(" Project generation completed.");
btnOpenFolder.Enabled = true;
if (chkDisableAnalytics.Checked)
{
string projectPath = Path.Combine(location, name);
string ngCmd = Path.Combine(projectPath, "node_modules", ".bin", "ng.cmd");
if (!File.Exists(ngCmd)) ngCmd = "ng";
WriteLine(">>> Disabling Angular analytics...");
await RunProcessAsync(ngCmd, "analytics off --global", projectPath, (line) => WriteLine(line));
}
if (chkBuildAfterCreate.Checked)
{
string projectPath = Path.Combine(location, name);
WriteLine(">>> Starting Angular build...");
string ngPath = Path.Combine(projectPath, "node_modules", ".bin", "ng.cmd");
int buildExit;
if (File.Exists(ngPath))
buildExit = await RunProcessAsync(ngPath, "build --no-interactive", projectPath, (line) => WriteLine(line));
else
buildExit = await RunProcessAsync("cmd.exe", "/c ng build --no-interactive", projectPath, (line) => WriteLine(line));
if (buildExit == 0) WriteLine("🏁 Angular build completed successfully.");
else WriteLine($"⚠️ Build exited with code {buildExit}.");
}
}
else
{
WriteLine($"❌ Process exited with code {exit}.");
}
}
catch (Exception ex)
{
progress.Style = ProgressBarStyle.Blocks;
WriteLine("❌ Error: " + ex.Message);
}
finally
{
ToggleUi(true);
}
}
private void ToggleUi(bool enabled)
{
foreach (Control c in this.Controls) c.Enabled = enabled;
btnOpenFolder.Enabled = false;
}
private void WriteLine(string? text)
{
if (string.IsNullOrEmpty(text)) return;
if (txtOutput.InvokeRequired) txtOutput.Invoke(new Action(() => txtOutput.AppendText(text + Environment.NewLine)));
else txtOutput.AppendText(text + Environment.NewLine);
}
private static Task<int> RunProcessAsync(string fileName, string arguments, string workingDir, Action<string?> onData)
{
var tcs = new TaskCompletionSource<int>();
var p = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = arguments,
WorkingDirectory = workingDir,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
},
EnableRaisingEvents = true
};
p.OutputDataReceived += (s, e) => onData(e.Data);
p.ErrorDataReceived += (s, e) => onData(e.Data);
p.Exited += (s, e) => { tcs.TrySetResult(p.ExitCode); p.Dispose(); };
try
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
return tcs.Task;
}
private void btnOpenFolder_Click(object sender, EventArgs e)
{
var target = Path.Combine(txtLocation.Text.Trim(), txtProjectName.Text.Trim());
if (Directory.Exists(target))
{
Process.Start(new ProcessStartInfo { FileName = target, UseShellExecute = true });
}
}
}
}
FormMain.Designer.cs
--------------------------------------
namespace AngularProjectWizard
{
partial class FormMain
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.lblHeader = new System.Windows.Forms.Label();
this.lblLocation = new System.Windows.Forms.Label();
this.txtLocation = new System.Windows.Forms.TextBox();
this.btnBrowseLocation = new System.Windows.Forms.Button();
this.lblProjectName = new System.Windows.Forms.Label();
this.txtProjectName = new System.Windows.Forms.TextBox();
this.lblNode = new System.Windows.Forms.Label();
this.txtNodePath = new System.Windows.Forms.TextBox();
this.btnBrowseNode = new System.Windows.Forms.Button();
this.lblCli = new System.Windows.Forms.Label();
this.txtAngularCliBase = new System.Windows.Forms.TextBox();
this.lblVersion = new System.Windows.Forms.Label();
this.txtAngularVersion = new System.Windows.Forms.TextBox();
this.chkStandalone = new System.Windows.Forms.CheckBox();
this.chkDefaults = new System.Windows.Forms.CheckBox();
this.chkBuildAfterCreate = new System.Windows.Forms.CheckBox();
this.chkDisableAnalytics = new System.Windows.Forms.CheckBox();
this.btnCreate = new System.Windows.Forms.Button();
this.txtOutput = new System.Windows.Forms.RichTextBox();
this.progress = new System.Windows.Forms.ProgressBar();
this.btnOpenFolder = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblHeader
//
this.lblHeader.AutoSize = true;
this.lblHeader.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold);
this.lblHeader.Location = new System.Drawing.Point(16, 12);
this.lblHeader.Name = "lblHeader";
this.lblHeader.Size = new System.Drawing.Size(203, 21);
this.lblHeader.TabIndex = 0;
this.lblHeader.Text = "New Angular CLI Project";
//
// lblLocation
//
this.lblLocation.AutoSize = true;
this.lblLocation.Location = new System.Drawing.Point(18, 50);
this.lblLocation.Name = "lblLocation";
this.lblLocation.Size = new System.Drawing.Size(53, 15);
this.lblLocation.TabIndex = 1;
this.lblLocation.Text = "Location";
//
// txtLocation
//
this.txtLocation.Location = new System.Drawing.Point(21, 68);
this.txtLocation.Name = "txtLocation";
this.txtLocation.Size = new System.Drawing.Size(560, 23);
this.txtLocation.TabIndex = 2;
//
// btnBrowseLocation
//
this.btnBrowseLocation.Location = new System.Drawing.Point(587, 68);
this.btnBrowseLocation.Name = "btnBrowseLocation";
this.btnBrowseLocation.Size = new System.Drawing.Size(75, 23);
this.btnBrowseLocation.TabIndex = 3;
this.btnBrowseLocation.Text = "Browse...";
this.btnBrowseLocation.UseVisualStyleBackColor = true;
this.btnBrowseLocation.Click += new System.EventHandler(this.btnBrowseLocation_Click);
//
// lblProjectName
//
this.lblProjectName.AutoSize = true;
this.lblProjectName.Location = new System.Drawing.Point(18, 99);
this.lblProjectName.Name = "lblProjectName";
this.lblProjectName.Size = new System.Drawing.Size(80, 15);
this.lblProjectName.TabIndex = 4;
this.lblProjectName.Text = "Project Name";
//
// txtProjectName
//
this.txtProjectName.Location = new System.Drawing.Point(21, 117);
this.txtProjectName.Name = "txtProjectName";
this.txtProjectName.Size = new System.Drawing.Size(220, 23);
this.txtProjectName.TabIndex = 5;
//
// lblNode
//
this.lblNode.AutoSize = true;
this.lblNode.Location = new System.Drawing.Point(18, 151);
this.lblNode.Name = "lblNode";
this.lblNode.Size = new System.Drawing.Size(94, 15);
this.lblNode.TabIndex = 6;
this.lblNode.Text = "Node interpreter";
//
// txtNodePath
//
this.txtNodePath.Location = new System.Drawing.Point(21, 169);
this.txtNodePath.Name = "txtNodePath";
this.txtNodePath.Size = new System.Drawing.Size(560, 23);
this.txtNodePath.TabIndex = 7;
//
// btnBrowseNode
//
this.btnBrowseNode.Location = new System.Drawing.Point(587, 169);
this.btnBrowseNode.Name = "btnBrowseNode";
this.btnBrowseNode.Size = new System.Drawing.Size(75, 23);
this.btnBrowseNode.TabIndex = 8;
this.btnBrowseNode.Text = "Browse...";
this.btnBrowseNode.UseVisualStyleBackColor = true;
this.btnBrowseNode.Click += new System.EventHandler(this.btnBrowseNode_Click);
//
// lblCli
//
this.lblCli.AutoSize = true;
this.lblCli.Location = new System.Drawing.Point(18, 203);
this.lblCli.Name = "lblCli";
this.lblCli.Size = new System.Drawing.Size(69, 15);
this.lblCli.TabIndex = 9;
this.lblCli.Text = "Angular CLI";
//
// txtAngularCliBase
//
this.txtAngularCliBase.Location = new System.Drawing.Point(21, 221);
this.txtAngularCliBase.Name = "txtAngularCliBase";
this.txtAngularCliBase.PlaceholderText = "npx --package @angular/cli";
this.txtAngularCliBase.ReadOnly = true;
this.txtAngularCliBase.Size = new System.Drawing.Size(329, 23);
this.txtAngularCliBase.TabIndex = 10;
this.txtAngularCliBase.Text = "npx --package @angular/cli";
//
// lblVersion
//
this.lblVersion.AutoSize = true;
this.lblVersion.Location = new System.Drawing.Point(364, 203);
this.lblVersion.Name = "lblVersion";
this.lblVersion.Size = new System.Drawing.Size(94, 15);
this.lblVersion.TabIndex = 11;
this.lblVersion.Text = "Angular CLI ver.";
//
// txtAngularVersion
//
this.txtAngularVersion.Location = new System.Drawing.Point(367, 221);
this.txtAngularVersion.Name = "txtAngularVersion";
this.txtAngularVersion.PlaceholderText = "latest or 20.3.4";
this.txtAngularVersion.Size = new System.Drawing.Size(214, 23);
this.txtAngularVersion.TabIndex = 12;
//
// chkStandalone
//
this.chkStandalone.AutoSize = true;
this.chkStandalone.Location = new System.Drawing.Point(21, 259);
this.chkStandalone.Name = "chkStandalone";
this.chkStandalone.Size = new System.Drawing.Size(221, 19);
this.chkStandalone.TabIndex = 13;
this.chkStandalone.Text = "Create new project with standalone";
this.chkStandalone.UseVisualStyleBackColor = true;
//
// chkDefaults
//
this.chkDefaults.AutoSize = true;
this.chkDefaults.Location = new System.Drawing.Point(21, 284);
this.chkDefaults.Name = "chkDefaults";
this.chkDefaults.Size = new System.Drawing.Size(171, 19);
this.chkDefaults.TabIndex = 14;
this.chkDefaults.Text = "Use the default project setup";
this.chkDefaults.UseVisualStyleBackColor = true;
//
// chkBuildAfterCreate
//
this.chkBuildAfterCreate.AutoSize = true;
this.chkBuildAfterCreate.Location = new System.Drawing.Point(21, 309);
this.chkBuildAfterCreate.Name = "chkBuildAfterCreate";
this.chkBuildAfterCreate.Size = new System.Drawing.Size(174, 19);
this.chkBuildAfterCreate.TabIndex = 19;
this.chkBuildAfterCreate.Text = "Run \"ng build\" after creation";
this.chkBuildAfterCreate.UseVisualStyleBackColor = true;
//
// chkDisableAnalytics
//
this.chkDisableAnalytics.AutoSize = true;
this.chkDisableAnalytics.Location = new System.Drawing.Point(21, 334);
this.chkDisableAnalytics.Name = "chkDisableAnalytics";
this.chkDisableAnalytics.Size = new System.Drawing.Size(206, 19);
this.chkDisableAnalytics.TabIndex = 20;
this.chkDisableAnalytics.Text = "Disable Angular analytics (global)";
this.chkDisableAnalytics.UseVisualStyleBackColor = true;
//
// btnCreate
//
this.btnCreate.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.btnCreate.Location = new System.Drawing.Point(587, 361);
this.btnCreate.Name = "btnCreate";
this.btnCreate.Size = new System.Drawing.Size(75, 30);
this.btnCreate.TabIndex = 15;
this.btnCreate.Text = "Create";
this.btnCreate.UseVisualStyleBackColor = true;
this.btnCreate.Click += new System.EventHandler(this.btnCreate_Click);
//
// txtOutput
//
this.txtOutput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.txtOutput.Font = new System.Drawing.Font("Consolas", 9F);
this.txtOutput.Location = new System.Drawing.Point(21, 407);
this.txtOutput.Name = "txtOutput";
this.txtOutput.ReadOnly = true;
this.txtOutput.Size = new System.Drawing.Size(641, 190);
this.txtOutput.TabIndex = 16;
this.txtOutput.Text = "";
//
// progress
//
this.progress.Location = new System.Drawing.Point(21, 361);
this.progress.Name = "progress";
this.progress.Size = new System.Drawing.Size(481, 8);
this.progress.TabIndex = 17;
//
// btnOpenFolder
//
this.btnOpenFolder.Location = new System.Drawing.Point(508, 361);
this.btnOpenFolder.Name = "btnOpenFolder";
this.btnOpenFolder.Size = new System.Drawing.Size(73, 30);
this.btnOpenFolder.TabIndex = 18;
this.btnOpenFolder.Text = "Open";
this.btnOpenFolder.UseVisualStyleBackColor = true;
this.btnOpenFolder.Click += new System.EventHandler(this.btnOpenFolder_Click);
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(684, 611);
this.Controls.Add(this.btnOpenFolder);
this.Controls.Add(this.progress);
this.Controls.Add(this.txtOutput);
this.Controls.Add(this.btnCreate);
this.Controls.Add(this.chkDisableAnalytics);
this.Controls.Add(this.chkBuildAfterCreate);
this.Controls.Add(this.chkDefaults);
this.Controls.Add(this.chkStandalone);
this.Controls.Add(this.txtAngularVersion);
this.Controls.Add(this.lblVersion);
this.Controls.Add(this.txtAngularCliBase);
this.Controls.Add(this.lblCli);
this.Controls.Add(this.btnBrowseNode);
this.Controls.Add(this.txtNodePath);
this.Controls.Add(this.lblNode);
this.Controls.Add(this.txtProjectName);
this.Controls.Add(this.lblProjectName);
this.Controls.Add(this.btnBrowseLocation);
this.Controls.Add(this.txtLocation);
this.Controls.Add(this.lblLocation);
this.Controls.Add(this.lblHeader);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "FormMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Angular CLI Project Wizard";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblHeader;
private System.Windows.Forms.Label lblLocation;
private System.Windows.Forms.TextBox txtLocation;
private System.Windows.Forms.Button btnBrowseLocation;
private System.Windows.Forms.Label lblProjectName;
private System.Windows.Forms.TextBox txtProjectName;
private System.Windows.Forms.Label lblNode;
private System.Windows.Forms.TextBox txtNodePath;
private System.Windows.Forms.Button btnBrowseNode;
private System.Windows.Forms.Label lblCli;
private System.Windows.Forms.TextBox txtAngularCliBase;
private System.Windows.Forms.Label lblVersion;
private System.Windows.Forms.TextBox txtAngularVersion;
private System.Windows.Forms.CheckBox chkStandalone;
private System.Windows.Forms.CheckBox chkDefaults;
private System.Windows.Forms.Button btnCreate;
private System.Windows.Forms.RichTextBox txtOutput;
private System.Windows.Forms.ProgressBar progress;
private System.Windows.Forms.Button btnOpenFolder;
private System.Windows.Forms.CheckBox chkBuildAfterCreate;
private System.Windows.Forms.CheckBox chkDisableAnalytics;
}
}
Program.cs
-----------------------
using System;
using System.Windows.Forms;
namespace AngularProjectWizard
{
internal static class Program
{
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new FormMain());
}
}
}
0 comments:
Post a Comment