startup code doesnt work

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1713 Views - Last Post: 16 August 2012 - 02:51 PM Rate Topic: -----

#1 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

startup code doesnt work

Posted 13 August 2012 - 12:13 PM

ok i have a code that copy my program to the startup foder everithing goes fine when im running the program it copy itself as it should but when im restart the computer its doing to me this eror

Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        private void Form1_Load(object sender, EventArgs e)
        {

            string src;
            string dest;
            string path = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup\WindowsFormsApplication1.exe";

            src = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            dest = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\" + System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
            File.Copy(src, dest, true);

        }

    }
}


and picture of the error
http://up351.siz.co....lmnzyntdujz.jpg

Is This A Good Question/Topic? 0
  • +

Replies To: startup code doesnt work

#2 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 12:21 PM

If your are already in startup, why are you trying to copy yourself again? Are you writing a virus that just keeps on trying to re-infect the machine over and over again? Even the virus writers learned not to do that back in the 90's because it made virus detection easier.
Was This Post Helpful? 0
  • +
  • -

#3 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 13 August 2012 - 12:30 PM

i dont want it to copy itself over and over that why i triend to do if file exist do nothing but its still did that error what to do?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 12:32 PM

I'm not seeing a file exist check in the code you posted in post #1.
Was This Post Helpful? 0
  • +
  • -

#5 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 13 August 2012 - 12:37 PM

i tried if it didnt worked so i deleted it.. is there any other way?
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l   User is offline

  • The Noid
  • member icon

Reputation: 1445
  • View blog
  • Posts: 2,057
  • Joined: 24-August 11

Re: startup code doesnt work

Posted 13 August 2012 - 12:40 PM

EDIT: Answered while I was typing.

This post has been edited by h4nnib4l: 13 August 2012 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#7 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 13 August 2012 - 12:58 PM

i didnt got answer what to do try catch?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 01:40 PM

Show us the code where you tried to check the file existing. Perhaps there was just a minor typo or an inverted boolean logic error.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 01:47 PM

The alternative to copying your entire executable to the Start Up folder is to copy a shortcut file into the start up folder. Right now I'm having brainlock trying to remember how to programmatically create shortcut file, but you've probably done it tons of times manually with the right click drag, or ALT left click drag. At least with the shortcut won't be trying to overwrite yourself.

Any which way, though, you'll want to fix the file exists check logic. It's a sign of poor craftsmanship to keep copying the same file over and over again if the file is already present.

A third option is to have your installer package add a shortcut to your program into the start up folder. This is what well polished programs do (after asking permission of course). That way it is only ever put in once, and uninstalling the program also correctly removes the shortcut.
Was This Post Helpful? 1
  • +
  • -

#10 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 13 August 2012 - 06:23 PM

I USED TRY CATCH NOW AND IT WORKS FINE THANK YOU
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 07:38 PM

No! No! No!

All you are doing is masking the problem. That means that you are just attempting to copy, failing the copy and proceeding on merrily.
Was This Post Helpful? 0
  • +
  • -

#12 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 13 August 2012 - 07:59 PM

if you want here is the code with the try and catch
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

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

        private void Form1_Load(object sender, EventArgs e)
        {
                try
                {
                    string src = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                    string dest = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\" + System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
                    File.Copy(src, dest, true);

                }
                catch (Exception e)
                {
                    MessageBox.Show(e);
                        
                    }
                }
            }
        }
    




Was This Post Helpful? -1
  • +
  • -

#13 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: startup code doesnt work

Posted 13 August 2012 - 08:02 PM

I'm still not seeing check to see if the file exists that you claim you have.
Was This Post Helpful? 0
  • +
  • -

#14 Boosi1   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 39
  • Joined: 29-July 12

Re: startup code doesnt work

Posted 14 August 2012 - 02:54 AM

i sayed to you that i deleted this check because it doesn't worked ass well
Was This Post Helpful? 0
  • +
  • -

#15 MrShoes   User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: startup code doesnt work

Posted 14 August 2012 - 04:36 AM

You shouldn't delete the check to see if the file's there, otherwise you will overwrite it every time. Bad practice.

What have you tried? The following should work (presuming a string is in fileName):
if(!File.Exists(fileName))
            {
                //Copy the file
            }

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2