c# updater help ( restart once update is complete ) - c#

Hello all i have managed to make a basic program that will do a update for me but one thing i want to try and do when the update form is open to close the main form then restart program when the update is finished but have no idea how to do this
this is the code i have currently
namespace update_test
{
public partial class Form1 : Form
{
// folder calls needed
string updatepath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "updatetest");
public Form1()
{
InitializeComponent();
}
// copying files
private static void DirectoryCopy(
string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, true);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
public void downloadfile()
{
//client.DownloadFile("http://elfenliedtopfan5.co.uk/update/elfenlied_weapons.zip", updatepath);
WebClient client = new WebClient();
string file = Path.Combine(updatepath, "elfenlied_weapons.zip");
client.DownloadFileAsync(new Uri("http://elfenliedtopfan5.co.uk/update/elfenlied_weapons.zip"), file);
client.DownloadProgressChanged += client_DownloadProgressChanged;
MessageBox.Show(file);
}
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
int bytesin = int.Parse(e.BytesReceived.ToString());
int totalbytes = int.Parse(e.TotalBytesToReceive.ToString());
int kb1 = bytesin / 1024;
int kb2 = totalbytes / 1024;
label1.Text = kb1.ToString() + "KB out of " + kb2.ToString() + "KB (" +e.ProgressPercentage.ToString() + "%)";
progressBar1.Value = e.ProgressPercentage;
if (e.ProgressPercentage == 100)
{
exactzip();
}
}
public void update()
{
string downloadurl = "";
Version newversion = null;
string xmlurl = "http://elfenliedtopfan5.co.uk/xml/update.xml";
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(xmlurl);
reader.MoveToContent();
string elementname = "";
if ((reader.NodeType == XmlNodeType.Element)&& (reader.Name == "update_test"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
elementname = reader.Name;
}
else
{
if((reader.NodeType == XmlNodeType.Text)&& (reader.HasValue))
{
switch (elementname)
{
case "version":
newversion = new Version(reader.Value);
break;
case "url":
downloadurl = reader.Value;
break;
}
}
}
}
}
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (reader != null)
reader.Close();
}
Version applicationvershion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (applicationvershion.CompareTo(newversion) < 0)
{
DialogResult dialogResult = MessageBox.Show("Version " + newversion.Major + "." + newversion.Minor + "." + newversion.Build + " of elfenliedprograms do you want to update now ?", "Update Avalible!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
var myForm = new Form1();
myForm.Show();
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
else
{
MessageBox.Show("program currently upto date :) ");
}
}
}
public void seeiffile()
{
// Set to folder path we must ensure exists.
string updatepathex = updatepath;
try
{
// If the directory doesn't exist, create it.
if (!Directory.Exists(updatepathex))
{
Directory.CreateDirectory(updatepathex);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
// if all above works then follow this
public void exactzip()
{
using (ZipFile zip = ZipFile.Read (Path.Combine(updatepath, "elfenlied_weapons.zip")))
{
foreach (ZipEntry e in zip)
{
e.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;
e.Extract(updatepath);
// e.Extract(updatepath);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
seeiffile();
downloadfile();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
ShowHideForm1(true);
}
private void ShowHideForm1(bool show)
{
Form1 f1 = Application.OpenForms.OfType<Form1>().SingleOrDefault();
if (f1 != null)
{
if (show)
{ f1.Show(); }
else
{ f1.Hide(); }
}
}
private void Form2_Shown(object sender, EventArgs e)
{
ShowHideForm1(false);
}
}
}
then i have another form attached to this application called
elfenliedprograms.cs
witch contains this code
namespace update_test
{
public partial class elfenliedprograms : Form
{
public elfenliedprograms()
{
InitializeComponent();
}
private void elfenliedprograms_Load(object sender, EventArgs e)
{
update();
}
public void update()
{
string downloadurl = "";
Version newversion = null;
string xmlurl = "http://elfenliedtopfan5.co.uk/xml/update.xml";
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(xmlurl);
reader.MoveToContent();
string elementname = "";
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "update_test"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
elementname = reader.Name;
}
else
{
if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
{
switch (elementname)
{
case "version":
newversion = new Version(reader.Value);
break;
case "url":
downloadurl = reader.Value;
break;
}
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (reader != null)
reader.Close();
}
Version applicationvershion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
if (applicationvershion.CompareTo(newversion) < 0)
{
DialogResult dialogResult = MessageBox.Show("Version "+ newversion.Major + "." + newversion.Minor + "." + newversion.Build + " of elfenliedprograms do you want to update now ?", "Update Avalible!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
var myForm = new Form1();
myForm.Show();
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
else
{
MessageBox.Show("program currently upto date :) ");
}
}
}
so basically once the update triggers there is a update it will call elfenliedprograms.cs but it will not shut down the main form if i do that it will shut the whole process down and im not sure how to go about making it download then restart app so all new stuff is updated it has taken me a good 4 months just to get this working the way i want not very good when it comes to updating a aplication so i would need help on this part sorry to be a pain and ask
thank you for

Related

Why when searching inside files for text when there are a lot of files to search the memory usage is raise over 2GB?

I had to shut down the app from task manager.
For example I'm searching the entire D:\ drive and there are over 400000 files.
The first time it's getting the files and the second time search in each file for a specific given string.
Starting a background worker:
private void startButton_Click(object sender, EventArgs e)
{
ListViewCostumControl.lvnf.Items.Clear();
numberoffiles = 0;
numberofrestrictedFiles = 0;
numberofdirs = 0;
label24.Text = "0";
label1.Text = "0";
label15.Text = "0";
Logger.Write("Operation started");
label21.Text = "Phase 1: Retrieving files";
label21.Visible = true;
startButton.Enabled = false;
stopButton.Enabled = true;
pauseresumeButton.Enabled = true;
label1.Select();
timer1.Start();
if (!backgroundWorker1.IsBusy)
{
SetWorkerMode(true);
backgroundWorker1.RunWorkerAsync();
}
}
Inside the DoWork:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
_stopwatch.Restart();
string[] values = textBox1.Text.Split(new string[] { ",," }, StringSplitOptions.None);
DirSearch(textBox3.Text, textBox2.Text, values, worker, e);
_stopwatch.Stop();
}
The DirSearch method:
int numberofdirs = 0;
void DirSearch(string rootDirectory, string filesExtension, string[] textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
List<string> resultsoftextfound = new List<string>();
List<string> resultsoftextfound1 = new List<string>();
List<string> filePathList = new List<string>();
int numberoffiles = 0;
try
{
filePathList = SearchAccessibleFilesNoDistinct(rootDirectory, null,worker,e).ToList();
}
catch (Exception err)
{
string ad = err.ToString();
}
label21.Invoke((MethodInvoker)delegate
{
label21.Text = "Phase 2: Searching in files";
});
MyProgress myp = new MyProgress();
myp.Report4 = filePathList.Count.ToString();
foreach (string file in filePathList)
{
try
{
var tempFR = File.ReadAllText(file);
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
bool reportedFile = false;
for (int i = 0; i < textToSearch.Length; i++)
{
if (tempFR.IndexOf(textToSearch[i], StringComparison.InvariantCultureIgnoreCase) >= 0)
{
resultsoftextfound.Add(file + " " + textToSearch[i]);
if (!reportedFile)
{
numberoffiles++;
myp.Report1 = file;
myp.Report2 = numberoffiles.ToString();
myp.Report3 = textToSearch[i];
backgroundWorker1.ReportProgress(0, myp);
reportedFile = true;
}
}
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = string.Format("{0}/{1}", numberofdirs, myp.Report4);
label1.Visible = true;
});
}
catch (Exception)
{
}
}
}
And the two recursive searching methods it's a bit long but all the methods are connected to each other:
int counter = 0;
IEnumerable<string> SearchAccessibleFilesNoDistinctOnlyExtension(string root, List<string> files)
{
if (files == null)
files = new List<string>();
if (Directory.Exists(root))
{
foreach (var file in Directory.EnumerateFiles(root))
{
string extension = Path.GetExtension(file);
if (!files.Contains(extension))
{
files.Add(extension);
counter++;
}
}
foreach (var subDir in Directory.EnumerateDirectories(root))
{
try
{
SearchAccessibleFilesNoDistinctOnlyExtension(subDir, files);
}
catch (UnauthorizedAccessException)
{
}
}
}
return files;
}
string restrictedFile = "";
List<string> restrictedFiles = new List<string>();
int numberofrestrictedFiles = 0;
int numberoffiles = 0;
IEnumerable<string> SearchAccessibleFilesNoDistinct(string root, List<string> files,BackgroundWorker worker, DoWorkEventArgs e)
{
_busy.WaitOne();
if (files == null)
files = new List<string>();
if (Directory.Exists(root))
{
foreach (var file in Directory.EnumerateFiles(root))
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
return files;
}
restrictedFile = file;
string ext = Path.GetExtension(file);
if (!files.Contains(file) && (ext == "." + textBox2.Text || textBox2.Text == "*"))
//if (!files.Contains(file) && ext == textBox2.Text)
{
files.Add(file);
}
numberoffiles++;
label24.Invoke((MethodInvoker)delegate
{
label24.Text = numberoffiles.ToString();
label24.Visible = true;
});
}
foreach (var subDir in Directory.EnumerateDirectories(root))
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
return files;
}
try
{
SearchAccessibleFilesNoDistinct(subDir, files,worker, e);
//files.Add(subDir);
}
catch (UnauthorizedAccessException)
{
restrictedFiles.Add(restrictedFile);
numberofrestrictedFiles++;
label11.Invoke((MethodInvoker)delegate
{
label11.Text = numberofrestrictedFiles.ToString();
label11.Visible = true;
});
continue;
}
}
}
return files;
}
It's working fine with small amount of files to search in.
But I want to be able to search also over a large size amount of files. For example 400000 files. The problem is that at some point it's taking all the ram usage 2GB memory usage even close to 3GB at some points.

create folder firing two times

My app two way sync app when iam created new folder from desktop it is some time creating extra folder with name "New folder "
private void FileSystemWatcherCreated(object sender, FileSystemEventArgs e)
{
try
{
var parentDirectory = Directory.GetParent(e.FullPath).FullName;
if (!this.IsWatchedObject(e.FullPath))
{
return;
}
if (!this.IsFileOrDirectoryExists(e.FullPath))
{
if (!string.IsNullOrWhiteSpace(e.FullPath))
{
this.Debug("FileSystemWatcherCreated", "Delete '{0}' from the icon overlay system...", e.FullPath);
var success = CloudFuzeIconOverlayAssystent.DeleteFileFolderRecord(e.FullPath, true, this.LogService);
this.Debug("FileSystemWatcherCreated", success.BooleanToSuccessFailLogString());
this.SendFileSystemItemChangedRecursively(e.FullPath);
}
return;
}
else
{
if (Directory.Exists(e.FullPath))
{ //http://stackoverflow.com/questions/16301598/filesystemwatcher-files-in-subdirectory
foreach (string file in Directory.GetFiles(e.FullPath))
{
var eventArgs = new FileSystemEventArgs(
WatcherChangeTypes.Created,
Path.GetDirectoryName(file),
Path.GetFileName(file));
FileSystemWatcherCreated(sender, eventArgs);
}
}
}
if (this.IsFileFolderAddedToRootFolder(this.cloudFuzeDirectory, e.FullPath))
{
return;
}
if (this.cloudFuzeDirectory == parentDirectory)
{
return;
}
var type = Directory.Exists(e.FullPath) ? WellKnownFileFoderTypes.Folder : WellKnownFileFoderTypes.File;
this.Debug("FileSystemWatcherCreated", "Add '{0}' in the queue for registration in the local storage...", e.FullPath);
this.addedFileFolders.Enqueue(new FileFolder
{
SavedFilePath = e.FullPath,
FileType = type,
Type = type
});
}
catch (Exception exc)
{
this.Error("FileSystemWatcherCreated", exc);
}
}

How to report progress on file output using progressbar in WinForms?

I made a program that changes a .txt file by using Writeline(). Everything works fine, just I really want to know whether it is possible or not to show the progress of the writing, or not, and if possible, how. Note: I only recently began with c#.
private void button1_Click(object sender, EventArgs e)
{
string res = "";
{
if (checkBox1.Checked == true) { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;gamefinished;"; }
else { res = "playercontrols:1:up-w,right-d,left-a,aimy-,run-lshift,reload-r,portal2-,portal1-,jump- ,aimx-,down-s,use-e;playercontrols:2:up-joy-1-hat-1-u,right-joy-1-hat-1-r,left-joy-1-hat-1-l,aimy-joy-1-axe-4-neg,run-joy-1-but-3,reload-joy-1-but-4,portal2-joy-1-but-6,portal1-joy-1-but-5,jump-joy-1-but-1,aimx-joy-1-axe-5-neg,down-joy-1-hat-1-d,use-joy-1-but-2;playercontrols:3:up-joy-2-hat-1-u,right-joy-2-hat-1-r,left-joy-2-hat-1-l,aimy-joy-2-axe-4-neg,run-joy-2-but-3,reload-joy-2-but-4,portal2-joy-2-but-6,portal1-joy-2-but-5,jump-joy-2-but-1,aimx-joy-2-axe-5-neg,down-joy-2-hat-1-d,use-joy-2-but-2;playercontrols:4:up-joy-3-hat-1-u,right-joy-3-hat-1-r,left-joy-3-hat-1-l,aimy-joy-3-axe-4-neg,run-joy-3-but-3,reload-joy-3-but-4,portal2-joy-3-but-6,portal1-joy-3-but-5,jump-joy-3-but-1,aimx-joy-3-axe-5-neg,down-joy-3-hat-1-d,use-joy-3-but-2;playercolors:1:224,32,0,136,112,0,252,152,56;playercolors:2:255,255,255,0,160,0,252,152,56;playercolors:3:0,0,0,200,76,12,252,188,176;playercolors:4:32,56,236,0,128,136,252,152,56;portalhues:1:0,0.125;portalhues:2:0.25,0.375;portalhues:3:0.5,0.625;portalhues:4:0.75,0.875;mariohats:1:1;mariohats:2:1;mariohats:3:1;mariohats:4:1;scale:3;shader1:none;shader2:none;volume:1;mouseowner:1;mappack:smb;"; }
if (checkBox2.Checked == true) { res = res + "reachedworlds:smb:1,"; }
else { res = res + "reachedworlds:smb:0,"; }
if (checkBox3.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox4.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox5.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox6.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox7.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox8.Checked == true) { res = res + "1,"; }
else { res = res + "0,"; }
if (checkBox9.Checked == true) { res = res + "1;"; }
else { res = res + "0;"; }
}
DialogResult ans=MessageBox.Show("Warning! All settings in your game will be reset. Do you wish to continue?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
if (ans == DialogResult.OK)
{
string roaming = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string path = roaming + "\\LOVE\\mari0\\options.txt";
using (StreamWriter sr = new StreamWriter(path))
{
sr.Write(res);
}
}
Assuming you're using winforms, based on one of your tags, here's something you can do:
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
}
private void WriteToFileMethod()
{
// your routine here
}
private void button_Click(object sender, EventArgs e)
{
progressBar.Maximum = 100;
progressBar.Step = 1;
progressBar.Value = 0;
backgroundWorker.RunWorkerAsync();
}
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
var backgroundWorker = sender as BackgroundWorker;
for (int i = 0; i < workSize; i++)
{
WriteToFileMethod();
backgroundWorker.ReportProgress((i * 100) / workSize);
}
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
Just replace workSize with the size of the work that you're trying to do. You can also adjust that code to report progress for number of lines that you're writing or number of files. It just depends on what exactly you're trying to report. You can even do memory size.
MSDN does a fantastic job of explaining all the details here. Besides looking at my example, I highly suggest reading up their overview. It'll provide a more detailed understanding of what needs to be done.
Lastly, progress bar might not always be a viable option. Something you can implement to let the user know that a background process is taking place and that the process hasn't crashed is a WaitCursor: Cursor.Current = Cursor.WaitCursor; Just something to think about...

Sending messages back to a client

I have a client-server applications I wrote in c#.
My server monitors the commands sent to it by the client.
I wish to send a "finish" message back to the client when the process finishes, I'm just not sure how to do it...
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Management;
using Microsoft.Win32;
namespace RM
{
public partial class RMFORM : Form
{
private Socket clientSock;
private string userName = "";
private string testName = "";
private string testNameString = "";
private string targetPath = "";
private bool isConnected = false;
private TestForm testForm;
private List<string> listOfFilesToCopy = new List<string>();
private List<string> listOfPathToCopy = new List<string>();
private RegistryKey registryKey;
public RMFORM ()
{
InitializeComponent();
userName = RemoteUtils.getConnectedUser();
targetPath = RemoteUtils.targetPath + userName;
testForm = new TestForm(RemoteUtils.remoteIpAddress, userName);
}
private void createNewCommand(string executable, string selectedPath, bool isDirectory)
{
string selectedOutDir = "";
testForm.enableFinishedButton();
testForm.setNumOfProcessesTest();
testForm.ShowDialog();
while (!testForm.getIsFinished())
{
if (testForm.getIsFormClosed())
{
testForm.Hide();
return;
}
}
testName = testForm.getTestName();
testNameString = testForm.getSelectedTestType();
selectedOutDir = targetPath + "\\" + testName;
try
{
if (Directory.Exists(selectedOutDir))
{
DialogResult dialogResult = MessageBox.Show("Test named " + testName + " already exists.\nDo You Wish To Continue?", "Warning!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.No)
{
return;
}
}
else
Directory.CreateDirectory(selectedOutDir);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
this.Close();
}
if (testNameString.CompareTo("Mek") == 0)
{
addSingleTest(executable, selectedPath, selectedOutDir, "mek", isDirectory);
}
if (testNameString.CompareTo("Mel") == 0)
{
addSingleTest(executable, selectedPath, selectedOutDir, "mel", isDirectory);
}
if (testNameString.CompareTo("Mem") == 0)
{
addSingleTest(executable, selectedPath, selectedOutDir, "mem", isDirectory);
}
}
private void addSingleTest(string executable, string selectedPath, string outputDir, string testType, bool isDirectory)
{
string commandToRun = "";
commandToRun = targetPath + RemoteUtils.batchRunPath + executable + testForm.getTestPerformance() + " " + selectedPath + " " + outputDir;
if (!isDirectory)
{
commandToRun += "\\" + Path.GetFileNameWithoutExtension(selectedPath) + "." + testType + ".pos " + testType;
}
else
{
commandToRun += " " + testType + " " + testForm.getNumOfProcess();
}
removeOne.Enabled = true;
removeAll.Enabled = true;
sendBtn.Enabled = true;
listORequestedCommands.Items.Add(commandToRun);
listORequestedCommands.Refresh();
}
private bool searchForFiles(string parentDirectory)
{
bool found = false;
if (Directory.GetFiles(parentDirectory, "*" + RemoteUtils.sequenceExtenssion).Length > 0)
return true;
try
{
foreach (string subDir in Directory.GetDirectories(parentDirectory))
{
if (Directory.GetFiles(subDir, "*" + RemoteUtils.sequenceExtenssion).Length > 0)
return true;
found = searchForFiles(subDir);
}
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
this.Close();
}
return found;
}
private void connectBtn_Click(object sender, EventArgs e)
{
try
{
if (!isConnected)
{
registryKey = Registry.CurrentUser.CreateSubKey(RemoteUtils.registrySubKey);
clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectBtn.Text = "Disconnect From Server";
connectBtn.Refresh();
clientSock.Connect(RemoteUtils.remoteIpAddress, RemoteUtils.remotePort);
statusColor.BackColor = Color.Green;
statusColor.Refresh();
isConnected = true;
buttonAddDirectory.Enabled = true;
buttonAddFile.Enabled = true;
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (sender1, e1) => RemoteUtils.copyDllsToServer(targetPath);
backgroundWorker.RunWorkerAsync();
}
else
{
registryKey.Close();
connectBtn.Text = "Connect To Server";
isConnected = false;
statusColor.BackColor = Color.Red;
buttonAddDirectory.Enabled = false;
buttonAddFile.Enabled = false;
clientSock.Close();
clientSock.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
this.Close();
}
}
private void sendBtn_Click(object sender, EventArgs e)
{
try
{
string [] commandsInRegistry = registryKey.GetValueNames();
for (int i = 0; i < commandsInRegistry.Length; i++)
{
registryKey.DeleteValue(commandsInRegistry[i]);
}
for (int i = 0; i < listORequestedCommands.Items.Count; i++)
{
clientSock.Send(Encoding.Default.GetBytes(listORequestedCommands.Items[i].ToString() + " <eom> "));
registryKey.SetValue("Command " + (i + 1).ToString(), listORequestedCommands.Items[i].ToString());
}
removeAll_Click(sender, e);
sendBtn.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
this.Close();
}
}
private void buttonAddDirectory_Click(object sender, EventArgs e)
{
folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.SelectedPath = "C:\\";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
string selectedPath = folderBrowserDialog.SelectedPath;
if (!searchForFiles(selectedPath))
{
MessageBox.Show("The directory: " + selectedPath + " doesn't contain sequences.", "Error!");
return;
}
testForm.enableNumOfProcesses();
createNewCommand(RemoteUtils.runBatchScript, selectedPath, true);
}
}
private void buttonAddFile_Click(object sender, EventArgs e)
{
openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";
openFileDialog.Filter = "PMD files (*" + RemoteUtils.sequenceExtenssion + ")|*" + RemoteUtils.sequenceExtenssion + "|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string selectedFile = openFileDialog.FileName;
if (Path.GetExtension(selectedFile).CompareTo(RemoteUtils.sequenceExtenssion) != 0)
{
MessageBox.Show("The file: " + selectedFile + " is not a sequence file.", "Error!");
return;
}
createNewCommand(RemoteUtils.batchRunExe, selectedFile, false);
}
}
private void removeOne_Click(object sender, EventArgs e)
{
int iRemoveIndex = listORequestedCommands.SelectedIndex;
if (iRemoveIndex > -1)
{
listORequestedCommands.Items.RemoveAt(iRemoveIndex);
listORequestedCommands.Refresh();
if (listORequestedCommands.Items.Count == 0)
{
removeOne.Enabled = false;
removeAll.Enabled = false;
sendBtn.Enabled = false;
}
}
}
private void removeAll_Click(object sender, EventArgs e)
{
listORequestedCommands.Items.Clear();
listORequestedCommands.Refresh();
buttonAddDirectory.Enabled = true;
buttonAddFile.Enabled = true;
removeOne.Enabled = false;
removeAll.Enabled = false;
sendBtn.Enabled = false;
}
}
}
Any ideas?
Have you looked at
http://www.thecodeproject.com/csharp/ZaSocks5Proxy.asp
We use code based on this in our s/w so that our "server" monitors input to broadcasts the message to all other users hooked into the "server"
In essence each workstation monitors in incoming string. The first word of the string we use as a "command" and process accordingly. "finish" could be such a command.

SaveFileDialog Menuclick Item Doesnt Work.

If I set path = "C:\\MSREAD.txt"; and Click on SaveAs Menu Item ,it saves Filetext,But If I dont give the String path and save it from saveFD.FileName it doesnt work.Please help me with this issue.
Thanks a lot
public void SaveToFile()
{
String SavedFile = "";
saveFD.InitialDirectory = #"C:";
saveFD.Title = "Save a Text File";
saveFD.FileName = "";
RichTextBox richTextBox1 = new RichTextBox();
saveFD.Filter = "Text Files|*.txt|All Files|*.*";
try
{
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
SavedFile = saveFD.FileName;
path = SavedFile.ToString();
//path = "C:\\MSREAD.txt";
MessageBox.Show(path);
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
SaveMyTextBoxContents(path);
}
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveToFile();
}
public void SaveMyTextBoxContents(string path)
{
if (listBoxItems.SelectedIndex == -1)
{
if (rdBtnSlow.Checked && rdBtnNo.Checked)
{
using (StreamWriter outputFile = new StreamWriter(path))
{
foreach (string item in listBoxItems.Items)
{
saveAllText = slowNo + " " + item;
outputFile.WriteLine(saveAllText);
}
}
}
}
}
Here is your problem:
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
SaveMyTextBoxContents(path);
You first save the richTextBox text to file, but then override the same file with SaveMyTextBoxContents, However the file is empty because of SaveMyTextBoxContents method will only save something if some conditions are true "not selected item and both check boxes are checked", and the listBoxItems.Items.Count > 0 which apparently not the case

Categories