0

Hello I am working on c# simple project.

This is the code:

    using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.IO;

 
private void SetStartup()
{
    RegistryKey rk = Registry.CurrentUser.OpenSubKey
        ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

    if (chkStartUp.Checked)
        rk.SetValue(AppName, Application.ExecutablePath);
    else
        rk.DeleteValue(AppName,false);            

}


namespace web1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
           
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // open browser

            Thread.Sleep(6000);
            System.Diagnostics.Process.Start("https://google.com/");


        }
    }
}

It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"

I want to: If I click on this program .exe, it should run on the startup (every startup). How to deal with it? I've tried so many ideas, but doesn't helped.

Thanks in advance. BTW: it's my first time here, nice to meet you all!

mattwass6
  • 1
  • 1
  • 1
    Does this answer your question? [How do I set a program to launch at startup](https://stackoverflow.com/questions/674628/how-do-i-set-a-program-to-launch-at-startup) – Martheen Dec 26 '21 at 12:07
  • It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly" – mattwass6 Dec 26 '21 at 12:11
  • Edit your question with the your error and modification – Martheen Dec 26 '21 at 12:40
  • @mattwass6 `AppName` is intended to be a string variable with the name of your application, for example "Mattwass6DrawingProgram". – Andrew Morton Dec 26 '21 at 12:59
  • You need to move your `private void SetStartup()` into your `class Form1`. Also **use a better name than `Form1`**. (Generally speaking), in C# all executable program code must exist inside a method or property body, and all methods/properties must exist in a `class` or `struct`. Since C# 10 you _can_ have "top-level code" without the outer `class` which I appreciate is useful for quick-and-dirty prototyping but I also think it's a bad idea overall ( https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/top-level-statements ) – Dai Dec 26 '21 at 13:08
  • Thanks guys! One more to go :) "CS0103 The name "chkStartUp" does not exist in the current context". – mattwass6 Dec 26 '21 at 13:16
  • @mattwass6 You didn't write this code, did you? – Dai Dec 26 '21 at 13:50
  • the code in first post? yes, I did it. But it is my "first time" with C#. Till today, I've been doing only python selenium automation stuff in case of programming. – mattwass6 Dec 26 '21 at 14:46

0 Answers0