Connect to VPN xamarin android - c#

Hello I am using this code to connect vpn with c# my code can connect and disconnect and create vpn and it working fine but i want use it in xamarin android i do search google but No result
private static string FolderPath => string.Concat(Directory.GetCurrentDirectory(),
"\\VPN");
private void btnConnect_Click(object sender, EventArgs e)
{
if (!Directory.Exists(FolderPath))
Directory.CreateDirectory(FolderPath);
var sb = new StringBuilder();
sb.AppendLine("[VPN]");
sb.AppendLine("MEDIA=rastapi");
sb.AppendLine("Port=VPN2-0");
sb.AppendLine("Device=WAN Miniport (IKEv2)");
sb.AppendLine("DEVICE=vpn");
sb.AppendLine("PhoneNumber=" + txtHost.Text);
File.WriteAllText(FolderPath + "\\VpnConnection.pbk", sb.ToString());
sb = new StringBuilder();
sb.AppendLine("rasdial \"VPN\" " + txtUsrname.Text + " " + txtPassword.Text + " /phonebook:\"" + FolderPath +
"\\VpnConnection.pbk\"");
File.WriteAllText(FolderPath + "\\VpnConnection.bat", sb.ToString());
var newProcess = new Process
{
StartInfo =
{
FileName = FolderPath + "\\VpnConnection.bat",
WindowStyle = ProcessWindowStyle.Normal
}
};
newProcess.Start();
newProcess.WaitForExit();
btnConnect.Enabled = false;
btnDisconnect.Enabled = true;
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
File.WriteAllText(FolderPath + "\\VpnDisconnect.bat", "rasdial /d");
var newProcess = new Process
{
StartInfo =
{
FileName = FolderPath + "\\VpnDisconnect.bat",
WindowStyle = ProcessWindowStyle.Normal
}
};
newProcess.Start();
newProcess.WaitForExit();
btnConnect.Enabled = true;
btnDisconnect.Enabled = false;
}
or I want convert it to xamarin android and
connect or disconnect sorry for my english

Related

Close a batch file ran by c#

I have a simpel program that runs a vulnerability scan on the given port and host. Now I have to find a way to close the batch file that is run from my c# form.
I have to be able to close the batch file from a button, even when its not finished yet. And i have no clue, nor did i find a way somewhere.
EDIT: Added more code, but still given the error "process doesn't exist in current context"
private void button10_Click(object sender, EventArgs e)
{
if (button10.Text == "Scan")
{
int port = (int)numericUpDown2.Value;
string path = Directory.GetCurrentDirectory();
string strCommand = path + "/SystemFiles/nikto/nikto.bat";
string host = textBox5.Text;
Console.WriteLine(strCommand);
richTextBox5.Text += "Starting Nikto Vulnerability Scan On " + host + " On Port " + port + System.Environment.NewLine;
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = strCommand;
startInfo.Arguments = "-h " + host + " -port " + port + textBox6.Text;
process.StartInfo = startInfo;
process.Start();
richTextBox5.Text += "Vulnerability Scan Started On " + host + " On Port " + port + System.Environment.NewLine;
button10.Text = "Cancel";
}
else
{
process.Kill();
button10.Text = "Scan";
}
}
You may use the method kill in the process object
process.Kill()
Here is the full answer.
public partial class Form1 : Form
{
private System.Diagnostics.Process _process;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (_process == null || _process.HasExited)
{
_process = new Process();
}
else
{
_process.Kill();
_process = null;
button10.Text = "Scan";
return;
}
int port = (int)numericUpDown2.Value;
string path = Directory.GetCurrentDirectory();
string strCommand = path + "/SystemFiles/nikto/nikto.bat";
string host = textBox5.Text;
Console.WriteLine(strCommand);
richTextBox5.Text += "Starting Nikto Vulnerability Scan On " + host + " On Port " + port + System.Environment.NewLine;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = strCommand,
Arguments = "-h " + host + " -port " + port + textBox6.Text
};
_process.StartInfo = startInfo;
_process.Start();
richTextBox5.Text += "Vulnerability Scan Started On " + host + " On Port " + port + System.Environment.NewLine;
button10.Text = "Cancel";
}
}

execute/open execute a program in c#

Hello so i created a kind of launcher application, but i have a but of a problem when i try to use the launch button it just doesn't do anything here's my code. What i need is a way to select the Arma folder trough the program that you have enterd but when i use the code it doesn't work?
private void iniSetup()
{
//Directory Read
tb_arma3dir.Text = ini.IniReadValue("Directory", "ARMA3");
tb_addondir.Text = ini.IniReadValue("Directory", "ADDON");
}
private void arma2OACheck()
{
string arma3Path = tb_arma3dir.Text;
if (arma3Path.EndsWith(#"\"))
arma3Path = arma3Path.TrimEnd('\'');
if(!File.Exists(arma3Path + "\\ArmA2OA.exe"))
{
a3dir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
a2Good = false;
}
else
{
a3dir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
a2Good = true;
}
}
private void addonDirCheck()
{
string addonPath = tb_addondir.Text;
if (addonPath.EndsWith(#"\"))
addonPath = addonPath.TrimEnd('\'');
if (!Directory.Exists(addonPath))
{
addondir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
addonGood = false;
}
else
{
addondir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
addonGood = true;
}
}
private void CompileLaunchParams()
{
string launchStr = "";
// Lauch parameters
launchStr += #"-mod=#TCG" + #" -ip=31.186.251.207" + #" -port=2302" + " -nosplash" + "-world=empty" + "-nosplash";
// Parse game path
string filename = "iniSetup";
// Start game process as administrator
ProcessStartInfo info = new ProcessStartInfo(filename); // what do i use to select the program so it can execute?
info.UseShellExecute = true;
info.Verb = "runas";
info.Arguments = launchStr;
Process.Start(info);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if(a2Good & addonGood ) // if the addon and arma is good it launches
{
CompileLaunchParams();
}
else
{
MessageBox.Show("Not all settings are configured, Please click the gear in the top right to define options!");
}
}
You can try save selected path to field in class like:
private string Arma3Path;
private void arma2OACheck()
{
string path = tb_arma3dir.Text;
if (path.EndsWith(#"\"))
Arma3Path = path.TrimEnd('\'') + "\\ArmA2OA.exe";
if(!File.Exists(Arma3Path))
{
a3dir_pic.Source = new BitmapImage(new Uri("./Resources/redX.png", UriKind.Relative));
a2Good = false;
}
else
{
a3dir_pic.Source = new BitmapImage(new Uri("./Resources/greenCheck.png", UriKind.Relative));
a2Good = true;
}
}
and then use it like
private void CompileLaunchParams()
{
string launchStr = "";
// Lauch parameters
launchStr += #"-mod=#TCG" + #" -ip=31.186.251.207" + #" -port=2302" + " -nosplash" + "-world=empty" + "-nosplash";
// Parse game path
string filename = Arma3Path;
// Start game process as administrator
ProcessStartInfo info = new ProcessStartInfo(filename); // what do i use to select the program so it can execute?
info.UseShellExecute = true;
info.Verb = "runas";
info.Arguments = launchStr;
Process.Start(info);
}

inconsistent thumbnail with ffmpeg on avi, mp4

i am getting inconsistent thumbanails when i add a new video. i have video files with width and height 640x480 and 1280x720. what do i have to change?
public void exec(string input, string output, string parametri)
{
ffmpeg = new Process();
ffmpeg.StartInfo.Arguments = " -i " + input + (parametri != null ? " " + parametri : "") + " " + output;
ffmpeg.StartInfo.FileName = "ffmpeg.exe";
ffmpeg.StartInfo.UseShellExecute = false;
ffmpeg.StartInfo.RedirectStandardOutput = true;
ffmpeg.StartInfo.RedirectStandardError = true;
ffmpeg.StartInfo.CreateNoWindow = true;
ffmpeg.Start();
ffmpeg.WaitForExit();
ffmpeg.Close();
}
public void GetThumbnail(string video, string jpg, string velicina)
{
if (velicina == null) velicina = "640x480";
exec(video, jpg, "-s " + velicina);
}
private void btnBrowseThumbnail_Click(object sender, EventArgs e)
{
string newThumbnail = Properties.Settings.Default.folderPathUserThumbnail + "CopyFiles.jpg";
GetThumbnail(txtFile.Text, newThumbnail, null);
string curFile = newThumbnail;
if (File.Exists(curFile))
txtThumbnail.Text = newThumbnail;
}

Expected behaviour with white space in the command line

I wrote a program that compiles my .cs files using csc.exe:
namespace myCompiler
{
public partial class Form1 : Form
{
string compilerFolder;
string outputFolder;
string projectFile;
string output = #" /out:";
public Form1()
{
InitializeComponent();
}
private void startCompile_Click(object sender, EventArgs e)
{
Compile();
}
public void findCompile_Click(object sender, EventArgs e)
{
DialogResult result1 = folderBrowserDialog1.ShowDialog();
compilerFolder = folderBrowserDialog1.SelectedPath;
MessageBox.Show(compilerFolder);
cscLabel.Text = compilerFolder;
}
private void outputCompile_Click(object sender, EventArgs e)
{
DialogResult result2 = folderBrowserDialog2.ShowDialog();
outputFolder = folderBrowserDialog2.SelectedPath;
outputLabel.Text = (outputFolder);
MessageBox.Show(outputFolder);
}
private void findProject_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
projectFile = openFileDialog1.FileName;
projectLabel.Text = (projectFile);
MessageBox.Show(projectFile);
}
}
public void Compile()
{
try
{
Process compile = new Process();
string outputExe = fileName.Text;
string compiler = compilerFolder + #"\csc.exe";
string arGs = output + outputFolder + #"\" + outputExe + " " + projectFile;
compile.StartInfo.FileName = (compiler);
compile.StartInfo.Arguments = (arGs);
compile.StartInfo.RedirectStandardOutput = true;
compile.StartInfo.UseShellExecute = false;
compile.Start();
string stdOutput = "";
while (!compile.HasExited)
{
stdOutput += compile.StandardOutput.ReadToEnd();
MessageBox.Show(stdOutput);
}
}
catch (Exception errorMsg)
{
MessageBox.Show(errorMsg.Message);
}
}
private void testButton_Click(object sender, EventArgs e)
{
MessageBox.Show(projectFile);
MessageBox.Show(compilerFolder);
}
}
}
The compile instruction runs but produces no results, just a quick flash of a black console screen.
Basically what seems to be happening, is when all the strings are parsed in the commandline as arguments for the process, the .cs project source directory is broken up by each white space ie c:\users\%username%\Documents\Visual Studio 2010\ is broken up as c:\users\%username%\Documents\Visual then Studio then 2010\Projects\Myproject\myproj.cs
and subsequently the compilation fails.
You need double quotes around a filepath with spaces in it.
See my edit to your code below.
public void Compile()
{
try
{
Process compile = new Process();
string outputExe = fileName.Text;
string compiler = compilerFolder + "\csc.exe";
// add double quotes around path with spaces in it.
string arGs = output + "\"" + outputFolder + "\"" + #"\" + outputExe + " " + projectFile;
compile.StartInfo.FileName = compiler;
compile.StartInfo.Arguments = arGs;
compile.StartInfo.RedirectStandardOutput = true;
compile.StartInfo.UseShellExecute = false;
compile.Start();
string stdOutput = "";
while (!compile.HasExited)
{
stdOutput += compile.StandardOutput.ReadToEnd();
MessageBox.Show(stdOutput);
}
}
catch (Exception errorMsg)
{
MessageBox.Show(errorMsg.Message);
}
}

how to get log file for multiple users in c#

public partial class Loginform : Form
{
public Loginform()
{
InitializeComponent();
}
private void btnlogin_Click(object sender, EventArgs e)
{
string dir = "D://Login.hhh";
if (!File.Exists(dir))
{
File.Create(dir);
}
string filePath = dir;
string s = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
string s1 = getIP();
using (StreamWriter swrObj = new StreamWriter(filePath, true))
{
swrObj.Write(s1 + "|" + s + "|" + Txtusername.Text + "|" + "user logged in on :|" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());
swrObj.Write("\t\t");
swrObj.Write("\t\t");
swrObj.WriteLine();
MessageBox.Show("Login success");
}
}
private void Loginform_Load(object sender, EventArgs e)
{
}
private string getIP()
{
IPHostEntry host;
string localIP = "?";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIP = ip.ToString();
}
}
return localIP;
}
}
I want to apply log file for project in C#, I am using above method to to create log file for getting system name, IP address and user login, but it is applicable in single user environment, if apply this in multi-user environment, How to get all users log info? please any one help me?
i assume that you want to create Log file for each user , then you can just do it like this:-
private void btnlogin_Click(object sender, EventArgs e)
{
string s1 = getIP();
string dir = "D://"+s1+"-"+DateTime.Now+"Login.hhh";
if (!File.Exists(dir))
{
File.Create(dir);
}
string filePath = dir;
string s = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
using (StreamWriter swrObj = new StreamWriter(filePath, true))
{
swrObj.Write(s1 + "|" + s + "|" + Txtusername.Text + "|" + "user logged in on :|" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString());
swrObj.Write("\t\t");
swrObj.Write("\t\t");
swrObj.WriteLine();
MessageBox.Show("Login success");
}
}

Categories

Resources