So I am writing a PGP application in C# using VS 2019. I am attempting to add the ability for encrypted file attachments, using 7-zip to encrypt the files by adding them to a password protected 7-zip archive. it appears the 7-zip visual studio tool does not on on VS 2019, so am attempting to call a Powershell script to launch 7-zip then encrypt the file chosen from the textbox. However, the script crashes, with this error (it is very long, so will be at the bottom).
Here is the code:
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.AspNetCore.Mvc;
using SharpCompress.Archives.SevenZip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UniveralWindowsTextPGP;
using static System.Net.Mime.MediaTypeNames;
namespace ByteOrbitPrivacyCannon
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
PowerShell ps = PowerShell.Create();
string script = string.Format("$process = C:\\Program Files\\7-Zip\\ $destinationFile = c:\\encryptedfile.zip $sourceFile = '{0}'", textBox1.Text, "$password = Read - Host '{0}'", Form2.verify, "Start - Process $process - ArgumentList a $destinationFile $sourceFile -p$password 7zip a -mx = 9 $Target $Source");
ps.AddScript(script);
ps.Invoke();
}
void button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select the file.";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
textBox1.Text = fdlg.FileName;
}
}
}
}
Please note Form2.verify is the password being used to lock the file and textBox1.text is the chosen file via the browser. Is there another way to call 7zip to do this in VS 2019, or how would this script be properly inserted into visual studio? Thanks very much.
Script error:
System.Management.Automation.ParseException
HResult=0x80131501
Message=System error.
Source=System.Management.Automation
StackTrace:
at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
at ByteOrbitPrivacyCannon.Form7.button2_Click(Object sender, EventArgs e) in C:\Users\keife\Downloads\ByteOrbitPrivacyCannon (2)\UniveralWindowsTextPGP\UniveralWindowsTextPGP\Form7.cs:line 35
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at UniveralWindowsTextPGP.Program.Main() in C:\Users\keife\Downloads\ByteOrbitPrivacyCannon (2)\UniveralWindowsTextPGP\UniveralWindowsTextPGP\Program.cs:line 19
User contributions licensed under CC BY-SA 3.0