[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

open txt file C#

I have created a 'Console Application' from Visual C# by following some instructions.  I need to add opening a file (which has the shipment info) and using the data in the file for the console application.
see the below example
instead of using 'Jack Hill' I want to use the information in  the file.  It might be best to just get the file into a listbox or an arrary.  I am not a c# programmer so i will need detail information.  I am using VS 2005

shipment.From.Name = "Jack Hill";
shipment.From.Addr01 = "321 Broken Crown Rd.";
shipment.From.City = "Roswell";
0
rjef
Asked:
rjef
  • 11
  • 6
  • 2
  • +1
2 Solutions
 
DooDahCommented:

http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx

namespace csharp_station.howto
{
    class TextFileReader
    {
        static void Main(string[] args)
        {
            // create reader & open file
            TextReader tr = new StreamReader("date.txt");

            // read a line of text
            Console.WriteLine(tr.ReadLine());

            // close the stream
            tr.Close();
        }
    }
}


using System;
using System.IO;

namespace csharp_station.howto
{
    class TextFileWriter
    {
        static void Main(string[] args)
        {
            // create a writer and open the file
            TextWriter tw = new StreamWriter("date.txt");

            // write a line of text to the file
            tw.WriteLine(DateTime.Now);

            // close the stream
            tw.Close();
        }
    }
}

0
 
rjefAuthor Commented:
so exactly where do i put that?  I am not a c# programmer so i need detailed instructions.
0
 
käµfm³d 👽Commented:
You can use the class that DooDah mentions (StreamReader) to get the information from your file. Of course, you will need to know the layout of your file in order to properly fill your object's fields:
public void GetFileData()
{
    using (System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\path\to\your\file.txt"))
    {
        // Reads the first three lines of the file and
        //  populates the fields of the object
        shipment.From.Name = reader.ReadLine();
        shipment.From.Addr01 = reader.ReadLine();
        shipment.From.City = reader.ReadLine();

        reader.Close();
    }
}

Open in new window

0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
Kusala WijayasenaSoftware EngineerCommented:
OK it seems to be you want to load the data in your file. isn't it ? Before reading the file, it is essential to know how data was organized in your file. Because way of reading is depend on that

So can you paste the content of your text file here (fraction of data)

-Kusala
0
 
käµfm³d 👽Commented:
Put the above within the curly braces of your "Program" class (assuming you haven't renamed it) and outside of the Main method. For example:
public class Program
{
    static void Main(string[] args)
    {
        GetFileData();
    }

    public static void GetFileData()
    {
        using (System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\path\to\your\file.txt"))
        {
            // Reads the first three lines of the file and
            //  populates the fields of the object
            shipment.From.Name = reader.ReadLine();
            shipment.From.Addr01 = reader.ReadLine();
            shipment.From.City = reader.ReadLine();

            reader.Close();
        }
    }
}

Open in new window

0
 
rjefAuthor Commented:
See below

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using XFlex.XShipService;


namespace XFlex
{
    class Program
    {
        static void Main(string[] args)

           
        {Console.WriteLine("Create XFlex Label...");
       

Shipment shipment = new Shipment();
shipment.CompanyKey = "xxxxxxx";
shipment.From = new Address();
shipment.From.Name = "Jack Hill";
shipment.From.Addr01 = "321 Broken Crown Rd.";
shipment.From.City = "Roswell";
shipment.From.State = "GA";
shipment.From.PostalCode = "30076";
shipment.From.Phone = "777-777-7777";
shipment.To = new Address();
shipment.To.Name = "Jill Hill";
shipment.To.Addr01 = "123 Tumbling St.";
shipment.To.Addr02 = "Suite A";
shipment.To.City = "Holland";
shipment.To.State = "MI";
shipment.To.PostalCode = "49424";
shipment.To.EMail = "jill.hill@over.com";
shipment.Description = "Pail of Water";
shipment.ReferenceNumber = "987654321";
shipment.Weight = 5;
Console.WriteLine("Created Shipment from Jack to Jill...");
try
{
using (XFlexShipClient client = new XFlexShipClient())
    {
ShipInfo info = client.ConfirmShipment(shipment);
Console.WriteLine("Tracking Number: {0}; X Number: {1}",
info.TrackingNumber, info.XNumber);
BinaryWriter bw = new BinaryWriter(File.Create(info.TrackingNumber + ".gif"));
bw.Write(Convert.FromBase64String(info.Label));
bw.Close();
Console.WriteLine("File: {0}.gif Created", info.TrackingNumber);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
        }
    }

0
 
käµfm³d 👽Commented:
We would *really* need to see an example of your file layout in order to give you a proper solution :)
0
 
rjefAuthor Commented:
the file would have the below info

companykey
From
FromName
FromAddr01
FromCity
FromState
FromPostalCode
FromPhone
ToName
ToAddr01
ToAddr02
ToCity
ToState
PostalCode
TOEMail
Description
ReferenceNumber
Weight

0
 
käµfm³d 👽Commented:
Is this series repeated (meaning can you have multiples of this sequence in one file) or is there one set of data per file?
0
 
rjefAuthor Commented:
that is the file.  i will create it with a vb app everytime i want to use it.  It will have 1 set of shipment info in it.  18 lines that's it.
0
 
rjefAuthor Commented:
Correction.  It is the below lines that will be in the file.
shipment.CompanyKey = "xxxxxxx";
shipment.From.Name = "Jack Hill";
shipment.From.Addr01 = "321 Broken Crown Rd.";
shipment.From.City = "Roswell";
shipment.From.State = "GA";
shipment.From.PostalCode = "30076";
shipment.From.Phone = "777-777-7777";
shipment.To.Name = "Jill Hill";
shipment.To.Addr01 = "123 Tumbling St.";
shipment.To.Addr02 = "Suite A";
shipment.To.City = "Holland";
shipment.To.State = "MI";
shipment.To.PostalCode = "49424";
shipment.To.EMail = "jill.hill@over.com";
shipment.Description = "Pail of Water";
shipment.ReferenceNumber = "987654321";
0
 
käµfm³d 👽Commented:
So maybe something like:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using XFlex.XShipService;


namespace XFlex
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Create XFlex Label...");

            try
            {
                using (System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\path\to\your\file.txt"))
                {
                    // Reads the first three lines of the file and
                    //  populates the fields of the object
                    Shipment shipment = new Shipment();
                    shipment.CompanyKey = reader.ReadLine();
                    shipment.From = new Address();
                    shipment.From.Name = reader.ReadLine();
                    shipment.From.Addr01 = reader.ReadLine();
                    shipment.From.City = reader.ReadLine();
                    shipment.From.State = reader.ReadLine();
                    shipment.From.PostalCode = reader.ReadLine();
                    shipment.From.Phone = reader.ReadLine();
                    shipment.To = new Address();
                    shipment.To.Name = reader.ReadLine();
                    shipment.To.Addr01 = reader.ReadLine();
                    shipment.To.Addr02 = reader.ReadLine();
                    shipment.To.City = reader.ReadLine();
                    shipment.To.State = reader.ReadLine();
                    shipment.To.PostalCode = reader.ReadLine();
                    shipment.To.EMail = reader.ReadLine();
                    shipment.Description = reader.ReadLine();
                    shipment.ReferenceNumber = reader.ReadLine();

                    reader.Close();
                }
            }
            catch   // Should "catch" if there is an error reading the file
                    //  (e.g. your file is shorter than18 lines
            {
                Console.WriteLine("Error creating label from Jack to Jill.");

                // Terminate app because there was an error reading
                return;
            }
            
            Console.WriteLine("Created Shipment from Jack to Jill...");

            try
            {
                using (XFlexShipClient client = new XFlexShipClient())
                {
                    ShipInfo info = client.ConfirmShipment(shipment);
                    Console.WriteLine("Tracking Number: {0}; X Number: {1}",
                    info.TrackingNumber, info.XNumber);
                    BinaryWriter bw = new BinaryWriter(File.Create(info.TrackingNumber + ".gif"));
                    bw.Write(Convert.FromBase64String(info.Label));
                    bw.Close();
                    Console.WriteLine("File: {0}.gif Created", info.TrackingNumber);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
    }
}

Open in new window

0
 
rjefAuthor Commented:
So the file will look like this.
 "xxxxxxx"
"Jack Hill"
 "321 Broken Crown Rd."
 "Roswell"
"GA"
"30076"
"777-777-7777"
"Jill Hill"
 "123 Tumbling St."
"Suite A"
 "Holland"
"MI"
"49424"
"jill.hill@over.com"
"Pail of Water"
 "987654321"
0
 
rjefAuthor Commented:
Error      1      The name 'shipment' does not exist in the current context      
0
 
Kusala WijayasenaSoftware EngineerCommented:
Remove "Shipment shipment = new Shipment();" in Line 22 and move it to Line 15 in the code that "kaufmed" has provided (in his last post)

-Kusala
0
 
rjefAuthor Commented:
one last thing.  How would i write the  reader.ReadLine line to the console so i could validate the file is inputing correctly.
0
 
rjefAuthor Commented:
never mind i think i got it.  Let me review all this and i will give my final answer in tommorow.
0
 
rjefAuthor Commented:
it's working.  Thanks alot
0
 
rjefAuthor Commented:
thanks you all are great.
0
 
käµfm³d 👽Commented:
NP. Glad to help :)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 11
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now