I can't figure out why the code don't work properly, when i click on save button show me Yokoso Log(1) then second save is showing Yokoso Log(1).txt(2).txt .....
//Create txt and write
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
TextWriter txtwrite = new StreamWriter(logPath);
int count = 1;
Find:
if (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
goto Find;
}
else
{
File.Create(logPath);
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
txtwrite.Close();
MessageBox.Show("Log create successfully (directory desktop).");
}
}
What you are trying to do is something like this:
var currentPath = logPath;
while (File.Exists(currentPath))
{
currentPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
File.Create(currentPath);
...
here you are creating a file
TextWriter txtwrite = new StreamWriter(logPath);
then when you you check for the file sure enough there is a file
if (File.Exists(logPath))
this is what I think you mean to do
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
int count = 1;
while (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
using (TextWriter txtwrite = new StreamWriter(logPath))
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
}
MessageBox.Show("Log create successfully (directory desktop).");
Related
I have the following code:
private void btnExcel_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Application XcelApp = new Microsoft.Office.Interop.Excel.Application();
XcelApp.Application.Workbooks.Add(Type.Missing);
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
try // catches null cells and ignores
{
for (int j = 0; j < dataGridView1.Columns.Count; j += 2)
{
XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
catch (NullReferenceException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.InnerException);
}
}
XcelApp.Columns.AutoFit();
XcelApp.Visible = true;
string pathway = #"C:\Temp\" + txtComputer.Text + "." + lblVersion.Text + "." + lblProduct.Text + ".csv";
XcelApp.GetSaveAsFilename(pathway, "CSV (Comma Delimited) (*.csv), *.csv",2, DialogResult.OK);
}
}
This outputs a datagridview to an excel spreadsheet, opens it and then opens the save file dialogue box to save the document as CSV.
I have several issues:
If I click save, it seems to ignore the save completely and no document is saved.
Ideally I'd like this to autosave, I'm not too bothered about it opening at all.
Excel.Application doesn't give a Save() or a SaveAs() method to do any of the above so I assume I am using the incorrect reference. What would I use so I can save this doc automatically?
Thanks
private void btnExcel_Click(object sender, EventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Application XcelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelWorkBook = XcelApp.Workbooks.Add();
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
XcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
try // catches null cells and ignores
{
for (int j = 0; j < dataGridView1.Columns.Count; j += 2)
{
XcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
catch (NullReferenceException)
{
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.InnerException);
}
}
string pathway = #"C:\Temp\" + txtComputer.Text + "." + lblVersion.Text + "." + lblProduct.Text + ".csv";
excelWorkBook.SaveAs(pathway, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV);
XcelApp.Workbooks.Close();
}
}
I am using Awesomium within C# and I am dragging variables out of the javascript within.
The code that is run:
if (GotCategories == false) {
if (WebBrowser.ExecuteJavascriptWithResult("finished").ToString() == "true" && WebBrowser.IsDocumentReady == true) {
int Level1Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories.length").ToString());
for (int i = 0; i < Level1Len - 1; i++) {
string Level1 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].title");
string Level1ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].id");
TreeNode treenode = new TreeNode(Level1);
CatsTree.Nodes.Add(treenode);
int Level2Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub.length").ToString());
for (int j = 0; j < Level2Len; j++) {
string Level2 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].title");
string Level2ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].id");
treenode = new TreeNode(Level2);
CatsTree.Nodes[i].Nodes.Add(treenode);
int Level3Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub.length").ToString());
for (int k = 0; k < Level3Len; k++) {
string Level3 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].title");
string Level3ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].id");
treenode = new TreeNode(Level3);
CatsTree.Nodes[i].Nodes[j].Nodes.Add(treenode);
if (WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].sub.length").IsInteger == true) {
string Level4Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].sub.length").ToString());
A weird issue happens though..
If I run it how it is now Level4Len throws an exception error
Input string was not in a correct format.
Though.. Weirdly enough if I put a MessageBox.Show(Level4Len); I do not receive the error and.. as well as that the outputs that are shown are completely normal as they should be.
Weirdest thing I have seen..
How can i make the below code more efficient, with less lines.
Im adding some PictureBox elements to a two dimensinal array.
int a = 0;
int b = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " a = " + a);
Console.WriteLine("Iteration: " + i + " b = " + b);
pictureBoxArr[a, b] = new PictureBox();
b++;
}
int aa = 1;
int bb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aa = " + aa);
Console.WriteLine("Iteration: " + i + " bb = " + bb);
pictureBoxArr[aa, bb] = new PictureBox();
bb++;
}
int aaa = 2;
int bbb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aaa = " + aaa);
Console.WriteLine("Iteration: " + i + " bbb = " + bbb);
pictureBoxArr[aaa, bbb] = new PictureBox();
bbb++;
}
I was thinking something like this - but im kinda stuck.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; i++)
{
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
You almost had it.
If you want something more re-usable, you could set up a couple variables to hold your bounds.
int boundX = 10;
int boundY = 10;
for (int i = 0; i < boundX ; i++)
{
for (int j = 0; j < boundY ; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
Try:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Iteration: " + i + " a = " + i);
Console.WriteLine("Iteration: " + j + " b = " + j);
pictureBoxArr[i, j] = new PictureBox();
}
}
Dude, you are very close
i was wondering how to acces a array outside a forloop.
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
for (int i = 0; i < 4; i++)
{
string[] linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
at this point, it does not recognize linesSplitted.
From your comment on another answer, I guess you want this:
string[] linesSplitted = new string[5];
for (int i = 0; i < 5; i++)
{
linesSplitted[i] = lines[i].Split(':')[1];
}
If this is not what you want, give us an example of the contents of the text file.
Declare the array outside of the loop. E.g.
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
string[] linesSplitted;
for (int i = 0; i < 4; i++)
{
linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
string[] linesSplitted;
for (int i = 0; i < 4; i++)
{
linesSplitted = lines[i].Split(':');
}
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
I'm not to sure about what you're trying to achieve there...
string[] lines = File.ReadAllLines("Customers/" + listBox1.SelectedItem.ToString() + "/" + listBox1.SelectedItem.ToString() + ".txt");
List<string[]> data = new List<string[]>();
for (int i = 0; i < 4; i++)
{
data.Add(lines[i].Split(':'));
}
//Retrive array from list and value from array and set to text box
TextboxName.Text = linesSplitted[0];
TextboxAddress.Text = linesSplitted[1];
TextboxZip.Text = linesSplitted[2];
TextboxTel.Text = linesSplitted[3];
TextboxEmail.Text = linesSplitted[4];
Thats my code . I want to use a faster sorting algorithm maybe quick sort or comb sort. i sorted the list twice first according to arrival then to brust time.
i need help implementing a faster sorting algorithm My main mwthod
static void Main(string[] args)
{
//----------------------------------------Reading I/O File--------------------------------------
string s = Environment.CurrentDirectory.ToString(); // returns the directory of the exe file
if (File.Exists(s + #"\input.txt")) //checking if the input files exists
Console.WriteLine("File Exists");
else
{
Console.WriteLine("File Not Found");
Console.WriteLine("-----------------------------------------------------");
return;
}
Console.WriteLine("-----------------------------------------------------");
//----------------------------------------Data Into List--------------------------------------
string FileText = File.ReadAllText(s + #"\input.txt"); //reading all the text in the input file
string[] lines = FileText.Split('\n'); //splitting the lines
List<Process> processes = new List<Process>();
for (int i = 1; i < lines.Length; i++)
{
string[] tabs = lines[i].Split('\t');//splitting the tabs to get objects' variables
Process x = new Process(tabs[0], int.Parse(tabs[1]), int.Parse(tabs[2]), int.Parse(tabs[3]));//creating object
processes.Add(x);//adding object to the list
}
// ----------------------------------------Sorting The List--------------------------------------
Process temp;
for (int k = 0; k < processes.Count; k++)
{
for (int i = k + 1; i < processes.Count; i++)
{
if (processes[k].arrivalTime > processes[i].arrivalTime)
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
}
int tempClock = 0;
for (int i = 0; i < processes.Count; i++)
{
if (processes[i].arrivalTime > tempClock)
tempClock = processes[i].arrivalTime;
for (int k = i + 1; k < processes.Count; k++)
{
if (processes[k].arrivalTime <= tempClock && processes[k].brust < processes[i].brust)
{
temp = processes[i];
processes[i] = processes[k];
processes[k] = temp;
}
}
tempClock += processes[i].brust;
}
Console.WriteLine("Processes After Sorting");
Console.WriteLine("-----------------------------------------------------");
Console.WriteLine("Name\tArrival\tBrust\tPriority");
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].priority);
Console.WriteLine();
}
Console.WriteLine("-----------------------------------------------------");
//----------------------------------------Gantt Chart--------------------------------------
Console.WriteLine("Gantt Chart");
Console.WriteLine("-----------------------------------------------------");
int counter = 0;
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t");
if (processes[i].arrivalTime < counter)
printSpaces(counter);
else
{
printSpaces(processes[i].arrivalTime);
counter = processes[i].arrivalTime;
}
printHashes(processes[i].brust);
counter += processes[i].brust;
Console.WriteLine();
}
Console.WriteLine("-----------------------------------------------------");
//-----------------------------------Completing Data And final Table-------------------------
int clock = 0, totalwait = 0, totalturnAround = 0;
for (int i = 0; i < processes.Count; i++)
{
if (processes[i].arrivalTime > clock)
{
processes[i].start = processes[i].arrivalTime;
clock += processes[i].start - processes[i].arrivalTime;
clock += processes[i].brust;
}
else
{
if (i > 0)
processes[i].start = processes[i - 1].end;
clock += processes[i].brust;
}
if (processes[i].start > processes[i].arrivalTime)
processes[i].wait = processes[i].start - processes[i].arrivalTime;
else processes[i].wait = 0;
processes[i].end = processes[i].start + processes[i].brust;
processes[i].turnAround = processes[i].wait + processes[i].brust;
totalwait += processes[i].wait;
totalturnAround += processes[i].turnAround;
}
Console.WriteLine("Name\tArrival\tBrust\tStart\tEnd\tWait\tturnaround");
for (int i = 0; i < processes.Count; i++)
{
Console.Write(processes[i].name + "\t" + processes[i].arrivalTime + "\t" + processes[i].brust + "\t" + processes[i].start + "\t" + processes[i].end + "\t" + processes[i].wait + "\t" + processes[i].turnAround);
Console.WriteLine();
}
double att = 0, awt = 0;
awt = (double)totalwait / (double)processes.Count;
att = (double)totalturnAround / (double)processes.Count;
Console.WriteLine("A.W.T= {0}", awt + "\t A.T.T= " + att);
Console.ReadKey();
}
Class Process
class Process
{
public Process(string name, int arrivalTime, int brust, int priority)
{
this.name = name;
this.arrivalTime = arrivalTime;
this.brust = brust;
this.priority = priority;
}
public Process()
{
}
public string name;
public int arrivalTime;
public int brust;
public int priority;
public int wait;
public int end;
public int start;
public int turnAround;
}
I would recommend you to have look at Parallel Sort Algorithm for inspiration.
I am also assuming that you want some kind of help implementing it, which with the above solution would be something like
// The contents of processes must implement IComparable<T>
QuicksortParallelOptimised(processes, 0, processes.Count);
But do please try to state a clear question :)
Adding the definition of the IComparable interface to your process-class;
partial class Process : IComparable<Process>
{
public override int CompareTo(Process otherProcess)
{
if (this.arrivalTime == otherProcess.arrivalTime)
return this.brust.CompareTo(otherProcess.brust);
return this.arrivalTime.CompareTo(otherProcess.brust);
}
}
Doing what I've stated so far would leave you with one round of sorting, and it would also make your code a hell of a lot more readable :)
If you have any questions just ask :)