-2

So I am attempting to code a script in C# VS Studio 2019, where a user inputs which file to encrypt using 7zip and choose their password etc, using string to decide a password and which file to encrypt. It will save to the c:\ drive as "encryptedfilehere.7z". The problem is I'm struggling to format the code correctly:

 private string button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Assuming 7-zip is installed in C:\\Program Files\\7-Zip\\.  If error message appears 7-zip is not installed in this driectory, it needs to be so.");
        string sourceName = textBox1.Text;
        string targetName = "c:\\encryptedmessagehere.7z";

        // 1
        // Initialize process information.
        //
        ProcessStartInfo p = new ProcessStartInfo();
        p.FileName = "C:\\Program Files\\7-Zip\\7z.exe";

        // 2
        // Use 7-zip
        // specify a=archive and -tgzip=gzip
        // and then target file in quotes followed by source file in quotes
        //
        p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\\" Form2.verify;
        p.WindowStyle = ProcessWindowStyle.Hidden;

        // 3.
        // Start process and wait for it to exit
        //
        Process x = Process.Start(p);
        x.WaitForExit();
    }
    

The password is Form2.verify because it takes the password the user entered in another form via the string called verify. The errors returned are:

Error CS0161 'Form7.button2_Click(object, EventArgs)': not all code paths return a value

For using Form2.verify as the password:

Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement ByteOribtPrivacyCannon C:\Users\keife\Downloads\ByteOrbitPrivacyCannon (2)\UniveralWindowsTextPGP\UniveralWindowsTextPGP\Form7.cs 48 Active

Any help is appreciated, thank you.

Keifer Bly
  • 47
  • 1
  • 4
  • The error message is quite plain. And the cause has been discussed to death on Stack Overflow. See duplicate for examples. For your second error (which is a completely different question...don't ask two questions in one post), do what you should have done for the first, which is to search the site (or just use your favorite web search) for the exact text of the error message. You will get a wealth of information. – Peter Duniho Sep 27 '20 at 19:44

1 Answers1

0

not all code paths return a value

There's no return statement in your code at all.

You don't know what to return? Then make the method return void instead of string. That's the correct signature for a button event handler anyway.

"\\" Form2.verify;

That needs a + to concatenate the string.

And BTW:

  • x.WaitForExit(); will just wait for 7Zip to end. It does not guarantee that something was compressed.
  • Having all that code in the event handler of the button might make your program unresponsive, depending on how long the Zip-operation takes. Windows may show the "not responding" title and the user might kill your application.
  • Consider making the path to 7Zip configurable instead of forcing people to install it in a particular location. You might even get the path from the Registry at HKEY_CLASSES_ROOT\7-Zip.7z\shell\open\command
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222