开发者

scheduling my application in Windows 2008

开发者 https://www.devze.com 2022-12-11 16:43 出处:网络
I wrote a console application which produces backup from my database and then compresses it to an rar file.

I wrote a console application which produces backup from my database and then compresses it to an rar file. it works fine except when it is scheduled in windows 2008. after windows runs my application,it says that it runs completely. but it doesn't compress my file . it only produces backup for me. by the way the windows is windows **server 2008** here is my code to convert backup file to rar file:

private static void ConvertBackupToRar(string backupFileName)
    {
        Console.WriteLine("Convert Backup to rar file");
        try
        {
            string compressStr = ConfigurationManager.AppSettings["BackupFolderAddress"].Trim() + @"Compress.bat";
            FileInfo fbat = new FileInfo(compressStr);
            StreamWriter fs = fbat.CreateText();
            fs.WriteLine("Winrar a " + back开发者_开发问答upFileName.Replace(".bak", ".rar") + " " + backupFileName);
            fs.Flush();
            fs.Close();

            System.Threading.Thread.Sleep(5000);
            using (System.Diagnostics.Process process1 = new System.Diagnostics.Process())
            {
                process1.StartInfo.FileName = compressStr;
                process1.Start();
            }
        }
        catch (Exception exp)
        {
            throw exp;
        }
    }


What account are you running the task as? That account will need to have winrar in its PATH.

You could emit the path into the batch file and/or resolve the path in some way in your C# code prior to emission.

If you're not going to do that, you should at least replace

  string compressStr = ConfigurationManager.AppSettings["BackupFolderAddress"].Trim() + @"Compress.bat";      

with :

  string compressStr = Path.Combine(ConfigurationManager.AppSettings["BackupFolderAddress"], @"Compress.bat");

Also, rather than generating a batch file to call winrar and invoking that, you could remove the indirection and do that directly - you have no error checking in the batch file and it's generally messy to introduce it as there's no exception handling. (And I'd use PS for this, but that's another story for another day...)

Have you tried sticking in a System.Diagnostics.Debugger.Break() into the code and debugging it?


Does it have to be a WinRar file? You can use GZipStream and DeflateStream classes which already exist within .NET to do you compression for you using streams. This would probably make things a little easier and you don't need to worry with tampering to the batch file.

0

精彩评论

暂无评论...
验证码 换一张
取 消