• Status: Solved
• Priority: Medium
• Security: Public
• Views: 226

# 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
• 11
• 6
• 2
• +1
2 Solutions

Commented:

namespace csharp_station.howto
{
{
static void Main(string[] args)
{
// create reader & open file

// read a line of text

// 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

Author Commented:
so exactly where do i put that?  I am not a c# programmer so i need detailed instructions.
0

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()
{
{
// Reads the first three lines of the file and
//  populates the fields of the object

}
}

0

Software 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

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()
{
{
// Reads the first three lines of the file and
//  populates the fields of the object

}
}
}

0

Author 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.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.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);
}
}
}
}

0

Commented:
We would *really* need to see an example of your file layout in order to give you a proper solution :)
0

Author Commented:
the file would have the below info

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

0

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

Author 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

Author 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.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

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
{
{
// Reads the first three lines of the file and
//  populates the fields of the object
Shipment shipment = new Shipment();

}
}
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);
}
}
}
}

0

Author 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

Author Commented:
Error      1      The name 'shipment' does not exist in the current context
0

Software 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

Author 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

Author Commented:
never mind i think i got it.  Let me review all this and i will give my final answer in tommorow.
0

Author Commented:
it's working.  Thanks alot
0

Author Commented:
thanks you all are great.
0

Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.