Search code examples
c#winformsadbopenfiledialogsavefiledialog

Getting full path location and sending it to CMD window


So I have been working on a way to save backups from adb and the best methord I came up with was this:

    saveFileDialog1.Title = "Save Backup";
    saveFileDialog1.OverwritePrompt = true;
    saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {   
        var process = Process.Start("CMD.exe", "/c adb backup -apk -all -f"+saveFileDialog1.FileName);
        process.WaitForExit();
    }

However it is not placing the file under the test name no matter where I save it. Am I doing something wrong? Its also the same for my openFileDialog:

openFileDialog1.Title = "Open Backup";
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var process = Process.Start("adb.exe", "restore"+openFileDialog1.FileName);
                process.WaitForExit();
            }

Solution

  • Your arguments are formatted incorrectly. You are missing a space in the following line:

    var process = Process.Start("CMD.exe", "/c adb backup -apk -all -f"+saveFileDialog1.FileName);
    

    -f"+saveFileDialog1.FileName should be -f "+saveFileDialog1.FileName instead.

    You should also wrap the filename in quotes to handle spaces in the file path:

    var process = Process.Start("CMD.exe", "/c adb backup -apk -all -f \""+saveFileDialog1.FileName+"\"");