Application/Process in C#

  • How to get the publish version in a .NET ClickOnce application:

You can use the ApplicationDeployment class to get the publish version of a .NET ClickOnce application. Here is an example:

if (ApplicationDeployment.IsNetworkDeployed)
{
    Version version = ApplicationDeployment.CurrentDeployment.CurrentVersion;
    Console.WriteLine("Publish version: " + version.ToString());
}
  • How to get .exe file version in C#:

You can use the FileVersionInfo class to get the version information of an executable file. Here is an example:

string exePath = @"C:\path\to\your\executable.exe";
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(exePath);
string version = versionInfo.FileVersion;
Console.WriteLine("Executable version: " + version);
  • How to get the current executable name of program in C#:

You can use the Path class to get the filename of the current executable file. Here is an example:

string executableName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
Console.WriteLine("Executable name: " + executableName);
  • How to get the current executable filename in C#:

You can use the Assembly class to get the filename of the current executable file. Here is an example:

string executableName = Assembly.GetEntryAssembly().Location;
Console.WriteLine("Executable filename: " + executableName);
  • How to check the application is 64-bit or 32-bit in C#:

You can use the Environment class to check whether the application is running in a 64-bit or 32-bit environment. Here is an example:

if (Environment.Is64BitProcess)
{
    Console.WriteLine("The application is running in a 64-bit environment.");
}
else
{
    Console.WriteLine("The application is running in a 32-bit environment.");
}
  • How to check the .NET Core is installed or not:

You can use the System.Runtime.InteropServices.RuntimeInformation class to check whether .NET Core is installed or not. Here is an example:

if (RuntimeInformation.FrameworkDescription.Contains(".NET Core"))
{
    Console.WriteLine(".NET Core is installed.");
}
else
{
    Console.WriteLine(".NET Core is not installed.");
}

Note that this check works for .NET Core 1.0 and later versions. If you need to check for earlier versions of .NET Core, you can use the System.Diagnostics.FileVersionInfo class to check the version number of the dotnet.exe executable.

  1. How to create a Windows Forms application in C#:

    To create a Windows Forms application in C#, use Visual Studio. Here's a simple example:

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp {
        public partial class MainForm : Form {
            public MainForm() {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e) {
                MessageBox.Show("Hello, Windows Forms!");
            }
        }
    
        static class Program {
            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainForm());
            }
        }
    }
    
  2. Multithreading in C# application:

    using System;
    using System.Threading;
    
    class Program {
        static void Main() {
            // Create and start a new thread
            Thread thread = new Thread(DoWork);
            thread.Start();
    
            // Do some work in the main thread
            for (int i = 0; i < 5; i++) {
                Console.WriteLine("Main Thread: " + i);
                Thread.Sleep(1000);
            }
    
            // Wait for the other thread to finish
            thread.Join();
        }
    
        static void DoWork() {
            for (int i = 0; i < 5; i++) {
                Console.WriteLine("Worker Thread: " + i);
                Thread.Sleep(1000);
            }
        }
    }
    
  3. C# console application example:

    using System;
    
    class Program {
        static void Main() {
            Console.WriteLine("Hello, C# Console Application!");
        }
    }
    
  4. Event handling in C# application:

    using System;
    using System.Windows.Forms;
    
    class Program {
        static void Main() {
            Button button = new Button();
            button.Click += Button_Click;
    
            MessageBox.Show("Click the button!");
    
            void Button_Click(object sender, EventArgs e) {
                MessageBox.Show("Button clicked!");
            }
        }
    }
    
  5. Asynchronous programming in C# application:

    using System;
    using System.Threading.Tasks;
    
    class Program {
        static async Task Main() {
            Console.WriteLine("Start");
    
            // Asynchronous method
            await DoAsyncWork();
    
            Console.WriteLine("End");
        }
    
        static async Task DoAsyncWork() {
            Console.WriteLine("Async Work Start");
            await Task.Delay(2000); // Simulate async work
            Console.WriteLine("Async Work End");
        }
    }
    
  6. Working with files in C# application:

    using System;
    using System.IO;
    
    class Program {
        static void Main() {
            string filePath = "example.txt";
    
            // Write to a file
            File.WriteAllText(filePath, "Hello, File!");
    
            // Read from a file
            string content = File.ReadAllText(filePath);
            Console.WriteLine(content);
        }
    }
    
  7. Database connectivity in C# application:

    using System;
    using System.Data.SqlClient;
    
    class Program {
        static void Main() {
            string connectionString = "Data Source=(local);Initial Catalog=YourDatabase;Integrated Security=True";
            using (SqlConnection connection = new SqlConnection(connectionString)) {
                connection.Open();
    
                // Execute SQL commands
                SqlCommand command = new SqlCommand("SELECT * FROM YourTable", connection);
                SqlDataReader reader = command.ExecuteReader();
    
                while (reader.Read()) {
                    Console.WriteLine($"{reader["ColumnName1"]}, {reader["ColumnName2"]}");
                }
            }
        }
    }