-1

I need to pass .zip file location into files parameter in the below c# code.

If the file name DOES NOT CONTAIN spaces, everything is working fine. But, if the file name DOES CONTAIN spaces, it is throwing below error.

Cannot find archive

Below is my code: Can anybody please suggest me how can I solve this problem?

        static void UnzipToFolder(string zipPath, string extractPath, string[] files)
    {
        string zipLocation = ConfigurationManager.AppSettings["zipLocation"];            

        foreach (string file in files)
        {
            string sourceFileName = string.Empty;
            string destinationPath = string.Empty;
            var name = Path.GetFileNameWithoutExtension(file);
            sourceFileName = Path.Combine(zipPath, file);
            destinationPath = Path.Combine(extractPath, name);

            var processStartInfo = new ProcessStartInfo();
            processStartInfo.FileName = zipLocation;
            processStartInfo.Arguments = @"x " + sourceFileName + " -o" + destinationPath;
            processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            var process = Process.Start(processStartInfo);
            process.WaitForExit();
        }
    }
Ashok kumar
  • 1,593
  • 4
  • 33
  • 68

2 Answers2

3

Add quotes around the file paths:

processStartInfo.Arguments = "x \"" + sourceFileName + "\" -o \"" + destinationPath + "\"";

Or for readability (with C# 6):

processStartInfo.Arguments = $"x \"{sourceFileName}\" -o \"{destinationPath}\"";
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

All filenames and paths which contain spaces must be quoted.

Next, regarding your question, how about stating the path like:

7z a -tzip C:\abc\zipfilename C:\"Program files"\DirectoryOrFile