Click here to Skip to main content
15,888,579 members
Home / Discussions / C#
   

C#

 
GeneralRe: The principle of programs like TeamViewer Pin
led mike18-May-09 11:52
led mike18-May-09 11:52 
QuestionDisplay Assembly Resources in Web Browser (problem with jpg/pdf) Pin
sodevrom10-May-09 7:41
sodevrom10-May-09 7:41 
AnswerRe: Display Assembly Resources in Web Browser (problem with jpg/pdf) Pin
Luc Pattyn10-May-09 8:09
sitebuilderLuc Pattyn10-May-09 8:09 
GeneralRe: Display Assembly Resources in Web Browser (problem with jpg/pdf) Pin
sodevrom10-May-09 8:15
sodevrom10-May-09 8:15 
GeneralRe: Display Assembly Resources in Web Browser (problem with jpg/pdf) Pin
Luc Pattyn10-May-09 11:00
sitebuilderLuc Pattyn10-May-09 11:00 
GeneralRe: Display Assembly Resources in Web Browser (problem with jpg/pdf) Pin
Luc Pattyn10-May-09 13:09
sitebuilderLuc Pattyn10-May-09 13:09 
GeneralRe: Display Assembly Resources in Web Browser (problem with jpg/pdf) Pin
sodevrom10-May-09 13:10
sodevrom10-May-09 13:10 
QuestionSelf-updating application by using batch files (Need Help) Pin
SimpleData10-May-09 6:28
SimpleData10-May-09 6:28 
Hi I want my application to update itself. As far as I know we can't do this in C# ( or any other language -without workarounds- ) because a file can't change itself while running.

I tried using another method which first downloads the newer executable, creates a batch file which waits for a while for the application to terminate then deletes the main app and replaces it with the newer one, then launches it.

But there is something wrong here I think. The batch file doesn't do everything correctly. Sometimes it doesn't delete itself or the update file, or sometimes newer does anything.

Class:
class updater
{
    public string UpdaterURL = null;
    public string LocalUpdaterLocation = null;

    public int ErrorCounter = 0;


    public void Download()
    {
        if (ErrorCounter == 0)
        {
            WebClient wc = null;
            try
            {
                wc = new WebClient();
                wc.DownloadFile(UpdaterURL, LocalUpdaterLocation);
            }
            catch { ErrorCounter++; }
            finally { if (wc != null) wc.Dispose(); }
        }
    }
    // TODO: Got to make sure that batch file is working correctly.
    public void CreateBatchFile()
    {
        if (ErrorCounter == 0)
        {
            string AppPath = Application.ExecutablePath.Replace(Application.StartupPath, "").Replace(@"\", "");

            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter("update.bat");
                sw.WriteLine("@echo off");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("ping 127.0.0.1 -n 2 -w 1000 > nul");
                sw.WriteLine("deleteexe:");
                sw.WriteLine(@"del """ + AppPath + @"""/Q/S>nul");
                sw.WriteLine(@"if exists """ + AppPath + @""" goto deleteexe");
                sw.WriteLine("rename " + LocalUpdaterLocation + " " + AppPath);
                sw.WriteLine(AppPath);
            }
            catch { ErrorCounter++; }
            finally { if (sw != null) { sw.Close(); sw.Dispose(); } }
        }
    }

    public void StartUpdating()
    {
        if (ErrorCounter == 0)
        {
            Process prc = new Process();
            prc.StartInfo.FileName = "update.bat";
            prc.StartInfo.ErrorDialog = false;
            prc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            prc.Start();
            Environment.Exit(0);
        }
    }
}


How I call it:
updater upd = new updater();

upd.UpdaterURL = "http://.../update.exe";
upd.LocalUpdaterLocation = "update.exe";
upd.ErrorCounter = 0;

upd.Download();
upd.CreateBatchFile();
upd.StartUpdating();


Thanks in advance,
Can
AnswerRe: Self-updating application by using batch files (Need Help) Pin
Giorgi Dalakishvili10-May-09 7:00
mentorGiorgi Dalakishvili10-May-09 7:00 
GeneralRe: Self-updating application by using batch files (Need Help) Pin
SimpleData10-May-09 7:22
SimpleData10-May-09 7:22 
Questionmultine textbox maxlenght depending of height and widht control Pin
ramaluciano10-May-09 4:39
ramaluciano10-May-09 4:39 
AnswerRe: multine textbox maxlenght depending of height and widht control Pin
Henry Minute10-May-09 5:13
Henry Minute10-May-09 5:13 
GeneralRe: multine textbox maxlenght depending of height and widht control Pin
ramaluciano10-May-09 5:45
ramaluciano10-May-09 5:45 
GeneralRe: multine textbox maxlenght depending of height and widht control Pin
Henry Minute10-May-09 6:37
Henry Minute10-May-09 6:37 
Questionprocess modifying/openning file Pin
lost_in_code10-May-09 2:27
lost_in_code10-May-09 2:27 
AnswerRe: process modifying/openning file Pin
Dave Kreskowiak10-May-09 6:04
mveDave Kreskowiak10-May-09 6:04 
Questionconverting the numeric value to text Pin
jaillani khan10-May-09 2:26
jaillani khan10-May-09 2:26 
AnswerRe: converting the numeric value to text Pin
Luc Pattyn10-May-09 2:34
sitebuilderLuc Pattyn10-May-09 2:34 
GeneralRe: converting the numeric value to text Pin
jaillani khan10-May-09 8:22
jaillani khan10-May-09 8:22 
QuestionNew form variables passing Pin
michaelgr110-May-09 1:15
michaelgr110-May-09 1:15 
AnswerRe: New form variables passing Pin
Henry Minute10-May-09 1:26
Henry Minute10-May-09 1:26 
GeneralRe: New form variables passing Pin
michaelgr110-May-09 1:34
michaelgr110-May-09 1:34 
GeneralRe: New form variables passing Pin
Henry Minute10-May-09 1:37
Henry Minute10-May-09 1:37 
GeneralRe: New form variables passing Pin
michaelgr110-May-09 1:47
michaelgr110-May-09 1:47 
GeneralRe: New form variables passing Pin
Colin Angus Mackay10-May-09 3:22
Colin Angus Mackay10-May-09 3:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.