views:

51

answers:

2

Hi everybody :-)

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 " + backupFileName.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;
        }
    }
A: 

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?

Ruben Bartelink
can you tell me what is PS?and how can I find some information about that?
Mehdi.KH
PowerShell - a command shell you use instead of cmd.exe which is essentially a .net language
Ruben Bartelink
Bottom line here is the simplest thing that could possibly work here is to figure out the right [absolute] path to your compressor and invoke that directly
Ruben Bartelink
thank youI used your suggestion and my problem was solvedprivate static void ConvertBackupToRar(string backupFileName){Assembly asm = Assembly.GetCallingAssembly();string rarAddress = System.IO.Path.GetDirectoryName(asm.Location) + "\\Rar.exe";using (Process p = new Process()) {p.StartInfo.UseShellExecute = false;p.StartInfo.RedirectStandardOutput = true;p.StartInfo.FileName = rarAddress;p.StartInfo.Arguments = string.Format("a {0} {1} ",backupFileName.Replace(".bak", ".rar"),backupFileName);p.Start();StreamReader reader = p.StandardOutputexeOutput = reader.ReadToEnd(); }
Mehdi.KH
Glad you got it sorted. BTW instead of `p.StartInfo.Arguments = string.Format("a {0} {1} ",backupFileName.Replace(".bak", ".rar"),backupFileName);` you can use Path.ChangeExtension
Ruben Bartelink
A: 

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.

Ian
WinRAR and 7-Zip provide much better compression, which is esp relevant if these files need to be ferried around quickly... (But I obviously agree with the basic idea of removing the indirection)
Ruben Bartelink
thank you.But,I didn't use those .NET class because Winrar compresses my file better.do you know any other class including opensource can compresses files?
Mehdi.KH
We need to get on at MS then to start extending their library :)
Ian
@Mehdi.KH: You dont want to manipulate RAR files (there is a 7-zip library that would do it, but thats beside the point). You want to *have* manipulated the RAR file. WinRAR or 7-Zip is the tool for that job. Hence all you want to do is call it. The other approach is to use SQL Enterprise 2008+'s compressed backups and you're almost there...
Ruben Bartelink
Thank youRight now ,There are some problems in our network.I'll check your suggestion later.
Mehdi.KH