ich bin noch blutiger Anfänger was C# und allgemein proggen angeht. Ich hab jetzt mein erstes Programm geschrieben, was genau das tut was ich gerne möchte aber der Code sieht in meinen Augen komisch aus und ständig brauchte ich die ganzen Variablen wieder. Ich habe mir alles selber beigebracht ohne Bücher sondern einfach nur durch die Dokumentation und Stackoverflow. Gibt es Möglichkeiten einigen Code in Methoden zu speichern ? Was kann man sonst noch so verbessern und was habe ich richtig bzw falsch gemacht in Bezug auf Ressourcen, Speed usw (auch wenn das prog eh nix verbraucht).
Naja hier ist der Code, würde mich freuen wenn man jemand drüber schaut :)
using Microsoft.Win32;
using System;
using System.IO;
using System.Windows.Forms;
namespace OperaProfileLauncher
{
public partial class OperaProfileLauncher : Form
{
public OperaProfileLauncher()
{
InitializeComponent();
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
private void Form1_Load(object sender, EventArgs e)
{
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\Profile";
string maindir = appdata + "" + "\\Opera Software\\Opera Stable";
bool exists = System.IO.Directory.Exists(pdir);
if (exists)
{
string[] files = Directory.GetFiles(pdir);
string[] dirs = Directory.GetDirectories(pdir);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
else
{
System.IO.Directory.CreateDirectory(pdir);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Nichts ausgewählt");
}
else
{
if (rbStable.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathStable = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last Stable Install Path", null);
string test = InstallPathStable + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/" + comboBox1.SelectedItem;
var cbox = comboBox1.SelectedItem;
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\Profile";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdir + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
if (rbBeta.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathBeta = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last beta Install Path", null);
string test = InstallPathBeta + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/" + comboBox1.SelectedItem;
var cbox = comboBox1.SelectedItem;
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdirBeta = appdata + "" + "\\Opera Software\\Opera Next\\Profile\\";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdirBeta + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
if (rbDev.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathDev = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last developer Install Path", null);
string test = InstallPathDev + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/" + comboBox1.SelectedItem;
var cbox = comboBox1.SelectedItem;
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdirDev = appdata + "" + "\\Opera Software\\Opera Developer\\Profile\\";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdirDev + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Nichts");
}
else
{
if (rbStable.Checked == true)
{
string folder = comboBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pathStable = appdata + "" + "\\Opera Software\\Opera Stable\\Profile\\" + folder;
string newpathStable = appdata + "" + "\\Opera Software\\Opera Stable\\Backup\\" + folder;
string path = pathStable;
string[] dirs = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
string newpath = newpathStable;
try
{
Directory.CreateDirectory(newpath);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
for (int j = 0; j < dirs.Length; j++)
{
try
{
Directory.CreateDirectory(dirs[j].Replace(path, newpath));
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
for (int j = 0; j < files.Length; j++)
{
try
{
File.Copy(files[j], files[j].Replace(path, newpath), true);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
if (rbBeta.Checked == true)
{
string folder = comboBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pathBeta = appdata + "" + "\\Opera Software\\Opera Next\\Profile\\" + folder;
// var s1 = @"""";
string newpathBeta = appdata + "" + "\\Opera Software\\Opera Next\\Backup\\" + folder;
string path = pathBeta;
string[] dirs = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
string newpath = newpathBeta;
try
{
Directory.CreateDirectory(newpath);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
for (int j = 0; j < dirs.Length; j++)
{
try
{
Directory.CreateDirectory(dirs[j].Replace(path, newpath));
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
for (int j = 0; j < files.Length; j++)
{
try
{
File.Copy(files[j], files[j].Replace(path, newpath), true);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
if (rbDev.Checked == true)
{
string folder = comboBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pathDeveloper = appdata + "" + "\\Opera Software\\Opera Developer\\Profile\\" + folder;
// var s1 = @"""";
string newpathDeveloper = appdata + "" + "\\Opera Software\\Opera Developer\\Backup\\" + folder;
string path = pathDeveloper;
string[] dirs = Directory.GetDirectories(path, "*.*", SearchOption.AllDirectories);
string newpath = newpathDeveloper;
try
{
Directory.CreateDirectory(newpath);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
for (int j = 0; j < dirs.Length; j++)
{
try
{
Directory.CreateDirectory(dirs[j].Replace(path, newpath));
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories);
for (int j = 0; j < files.Length; j++)
{
try
{
File.Copy(files[j], files[j].Replace(path, newpath), true);
}
catch (IOException ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click_1(object sender, EventArgs e)
{
}
private void button2_Click_2(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("Bitte Namen angeben.");
}
else
{
if (rbStable.Checked == true)
{
string folder = textBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\Profile\\";
string pdir2 = pdir + folder;
if (Directory.Exists(pdir2))
{
MessageBox.Show("Profil schon Vorhanden.");
}
else
{
comboBox1.Items.Add(textBox1.Text);
DirectoryInfo di = Directory.CreateDirectory(pdir2);
comboBox1.SelectedIndex = comboBox1.FindStringExact(textBox1.Text);
if (comboBox1.Items.Contains(comboBox1.Items))
{
string[] files = Directory.GetFiles(pdir);
string[] dirs = Directory.GetDirectories(pdir);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
}
}
if (rbBeta.Checked == true)
{
string folder = textBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdirBeta = appdata + "" + "\\Opera Software\\Opera Next\\Profile\\";
string pdir2 = pdirBeta + folder;
if (Directory.Exists(pdir2))
{
MessageBox.Show("Profil schon Vorhanden.");
}
else
{
comboBox1.Items.Add(textBox1.Text);
DirectoryInfo di = Directory.CreateDirectory(pdir2);
comboBox1.SelectedIndex = comboBox1.FindStringExact(textBox1.Text);
if (comboBox1.Items.Contains(comboBox1.Items))
{
string[] files = Directory.GetFiles(pdirBeta);
string[] dirs = Directory.GetDirectories(pdirBeta);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
}
}
if (rbDev.Checked == true)
{
string folder = textBox1.Text;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdirDev = appdata + "" + "\\Opera Software\\Opera Developer\\Profile\\";
string pdir2 = pdirDev + folder;
if (Directory.Exists(pdir2))
{
MessageBox.Show("Profil schon Vorhanden.");
}
else
{
comboBox1.Items.Add(textBox1.Text);
DirectoryInfo di = Directory.CreateDirectory(pdir2);
comboBox1.SelectedIndex = comboBox1.FindStringExact(textBox1.Text);
if (comboBox1.Items.Contains(comboBox1.Items))
{
string[] files = Directory.GetFiles(pdirDev);
string[] dirs = Directory.GetDirectories(pdirDev);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
}
}
}
}
private void tArg_TextChanged(object sender, EventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show("DU klickst button");
}
private void bDelete_Click(object sender, EventArgs e)
{
var s2 = comboBox1.SelectedItem;
var cbox = comboBox1.SelectedItem;
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\Profile\\";
string pdirBeta = appdata + "" + "\\Opera Software\\Opera Next\\Profile\\";
string pdirDev = appdata + "" + "\\Opera Software\\Opera Developer\\Profile\\";
string folder = textBox1.Text;
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("Nichts ausgewählt");
}
else
{
if (rbStable.Checked == true)
{
DialogResult result = MessageBox.Show("Willst du das Profil wirklich löschen ?",
"Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
comboBox1.Items.Remove(s2);
Directory.Delete(pdir + s2, true);
MessageBox.Show("Profil Entfernt!");
}
catch { }
}
}
else { }
if (rbBeta.Checked == true)
{
DialogResult result = MessageBox.Show("Willst du das Profil wirklich löschen ?",
"Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
comboBox1.Items.Remove(s2);
Directory.Delete(pdirBeta + s2, true);
}
catch (Exception) { }
}
}
else { }
if (rbDev.Checked == true)
{
DialogResult result = MessageBox.Show("Willst du das Profil wirklich löschen ?",
"Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
try
{
comboBox1.Items.Remove(s2);
Directory.Delete(pdirDev + s2, true);
MessageBox.Show("Profil Entfernt!");
}
catch (Exception) { }
}
}
}
}
private void rbStable_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\Profile";
bool exists = System.IO.Directory.Exists(pdir);
if (exists)
{
string[] files = Directory.GetFiles(pdir);
string[] dirs = Directory.GetDirectories(pdir);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
}
private void rbBeta_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Next\\Profile";
bool exists = System.IO.Directory.Exists(pdir);
if (exists)
{
string[] files = Directory.GetFiles(pdir);
string[] dirs = Directory.GetDirectories(pdir);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
}
private void rbDev_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Items.Clear();
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Developer\\Profile";
bool exists = System.IO.Directory.Exists(pdir);
if (exists)
{
string[] files = Directory.GetFiles(pdir);
string[] dirs = Directory.GetDirectories(pdir);
foreach (string item2 in dirs)
{
FileInfo f = new FileInfo(item2);
comboBox1.Items.Add(f.Name);
}
foreach (string item in files)
{
FileInfo f = new FileInfo(item);
comboBox1.Items.Add(f.Name);
}
}
else
{
System.IO.Directory.CreateDirectory(pdir);
}
}
private void bMain_Click(object sender, EventArgs e)
{
if (rbStable.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathStable = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last Stable Install Path", null);
string test = InstallPathStable + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/";
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Stable\\";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdir + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
if(rbBeta.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathBeta = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last beta Install Path", null);
string test = InstallPathBeta + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/";
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Next\\";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdir + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
if (rbDev.Checked == true)
{
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
string InstallPathDev = (string)Registry.GetValue(@"HKEY_CURRENT_USER\Software\Opera Software", "Last developer Install Path", null);
string test = InstallPathDev + @"launcher.exe";
pProcess.StartInfo.FileName = test;
var s1 = @"""";
var s2 = "/";
string arg = "--user-data-dir=";
string appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string pdir = appdata + "" + "\\Opera Software\\Opera Developer\\";
pProcess.StartInfo.Arguments = arg + "" + s1 + "" + pdir + "" + s2 + "" + s1;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true;
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd();
pProcess.WaitForExit();
}
}
}
}