-2

So, I am busy making a c# WinForms application for my company. For this, I need to be able to save my tickets in data with the application. Here is what the ticket dialog looks like: enter image description here

I want to save all the data, in some file stored locally (.txt may be the best?) Line by line, like so:

enter image description here

The application should also be able to open the file and show the data in the form.

I should probably use the System.IO.Filenamespace but I have no experience with it. My google searches couldn't help me out with this issue, so I reached out to stackoverflow.

Jort D.
  • 25
  • 1
  • 2
  • 1
    What have you tried so far? Show us some code, please. – Rui Jarimba Jun 24 '18 at 13:40
  • its so easy to read the [documentation](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file) – styx Jun 24 '18 at 13:41
  • 3
    You really don't want to save data in plain text. At the very least, you should use json or xml. It's going to be much easier to write and maintain. – Zohar Peled Jun 24 '18 at 13:42
  • Well, for saving, you can store the values in an array, then loop through the array and File.WriteLine will do the job. For receiving, I think the same concept will do the job – Kaj Jun 24 '18 at 13:43
  • Look into NewtonSoft. Serialize the info and write it to a file. Then during reading, you can deserialize it and display and/or edit the info. – CodingYoshi Jun 24 '18 at 13:54
  • Look at File.WriteAllText in .Net and File.AppendAllText These simplified functions are useful for trivial or proof of concept scenarios. They are rarely used in production code so your google searches will not find them easily. Start thinking about your read file operation, that will impact how you write the file out - good luck and have fun. – Sql Surfer Jun 24 '18 at 14:19

2 Answers2

0

The first WriteLine code with an array might help you. So you would create an string array with all informations provided and then write it to the file.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-write-to-a-text-file

Philipp.

-1

Try code like below to create an xml file

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.Xml;
using System.Xml.Linq;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const string FILENAME = @"c:\temp\test.xml";
        XDocument doc = null;
        XElement tickets = null;
        public Form1()
        {
            InitializeComponent();
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);

            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tickets></tickets>";
            doc = XDocument.Parse(ident);
            tickets = doc.Root;

        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            doc.Save(FILENAME);
        }

        private void addTicket_Click(object sender, EventArgs e)
        {
            XElement newTicket = new XElement("ticket",
                new XElement("ID", this.ID),
                new XElement("Name", this.Name),
                new XElement("Type", this.Type),
                new XElement("Device_Name", this.Device_Name),
                new XElement("Serial_Number", this.Number),
                new XElement("Repair_Data", this.Repair),
                new XElement("Fix", this.Fix),
                new XElement("Additional", this.Additional)
                );
            tickets.Add(newTicket);
        }

    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 3
    Providing ready solutions without questioning OP about their efforts is promoting blind copy-paste. SO is not and should never become a code writing service. Just my opinion. – Nikhil Vartak Jun 24 '18 at 14:08
  • 1
    Novice don't have the ability to write the code. So why waste anybodies time. Often it takes me less time to write the code then to ask the questions. In this case the person doesn't even know what xml is or the advantages. Does it make sense to have them write text data and then to correct them with an xml solution? – jdweng Jun 24 '18 at 16:04