patterncsharpMinor
Amature SVN review
Viewed 0 times
amaturereviewsvn
Problem
I have written a simple copy file code, called 'Amature SVN', kindly review my code.
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace AmatureSVN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSelectFolder_Click(object sender, EventArgs e)
{
DialogResult result = dirBrowserDlg.ShowDialog();
if (result == DialogResult.OK)
{
txtSelectedFolder.Text = dirBrowserDlg.SelectedPath;
}
}
private void btnSelectFiles_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string[] selectedFiles = openFileDialog1.FileNames;
foreach (string str in selectedFiles)
{
//unique items
if (listboxSelectedFiles.Items.Contains(str)) continue;
listboxSelectedFiles.Items.Add(str);
}
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
string backupDir = CreateBackupDir();
//if dir not created return
if (string.IsNullOrEmpty(backupDir)) return;
CopyFilesToDir(backupDir);
//Create Readme
if (!string.IsNullOrEmpty(txtReadMe.Text))
CreateReadMe(backupDir);
}
private void CreateReadMe(string backupDir)
{
using (StreamWriter writer = new StreamWriter(backupDir + @"\ReadMe.txt",false))
{
try
{
writer.Write(txtReadMe.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
string CreateBackupDir()
{
//create backup fo
```
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace AmatureSVN
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnSelectFolder_Click(object sender, EventArgs e)
{
DialogResult result = dirBrowserDlg.ShowDialog();
if (result == DialogResult.OK)
{
txtSelectedFolder.Text = dirBrowserDlg.SelectedPath;
}
}
private void btnSelectFiles_Click(object sender, EventArgs e)
{
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
string[] selectedFiles = openFileDialog1.FileNames;
foreach (string str in selectedFiles)
{
//unique items
if (listboxSelectedFiles.Items.Contains(str)) continue;
listboxSelectedFiles.Items.Add(str);
}
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
string backupDir = CreateBackupDir();
//if dir not created return
if (string.IsNullOrEmpty(backupDir)) return;
CopyFilesToDir(backupDir);
//Create Readme
if (!string.IsNullOrEmpty(txtReadMe.Text))
CreateReadMe(backupDir);
}
private void CreateReadMe(string backupDir)
{
using (StreamWriter writer = new StreamWriter(backupDir + @"\ReadMe.txt",false))
{
try
{
writer.Write(txtReadMe.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
string CreateBackupDir()
{
//create backup fo
Solution
There isn't much too review. A few thing things that can be improved are:
-
Reduce nesting by inverting
with
- Use
Path.Combine()instead of building file paths manually.
- Use
String.Emptyinstead of"".
-
Reduce nesting by inverting
if statements, e.g. by replacing if (result == DialogResult.OK)
{...}with
if (result != DialogResult.OK)
return;Code Snippets
if (result == DialogResult.OK)
{...}if (result != DialogResult.OK)
return;Context
StackExchange Code Review Q#32376, answer score: 5
Revisions (0)
No revisions yet.