rjef
asked on
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";
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";
ASKER
so exactly where do i put that? I am not a c# programmer so i need detailed instructions.
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();
}
}
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
So can you paste the content of your text file here (fraction of data)
-Kusala
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();
}
}
}
ASKER
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(shi pment);
Console.WriteLine("Trackin g Number: {0}; X Number: {1}",
info.TrackingNumber, info.XNumber);
BinaryWriter bw = new BinaryWriter(File.Create(i nfo.Tracki ngNumber + ".gif"));
bw.Write(Convert.FromBase6 4String(in fo.Label)) ;
bw.Close();
Console.WriteLine("File: {0}.gif Created", info.TrackingNumber);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa ge);
}
Console.ReadLine();
}
}
}
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
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
try
{
using (XFlexShipClient client = new XFlexShipClient())
{
ShipInfo info = client.ConfirmShipment(shi
Console.WriteLine("Trackin
info.TrackingNumber, info.XNumber);
BinaryWriter bw = new BinaryWriter(File.Create(i
bw.Write(Convert.FromBase6
bw.Close();
Console.WriteLine("File: {0}.gif Created", info.TrackingNumber);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa
}
Console.ReadLine();
}
}
}
We would *really* need to see an example of your file layout in order to give you a proper solution :)
ASKER
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
companykey
From
FromName
FromAddr01
FromCity
FromState
FromPostalCode
FromPhone
ToName
ToAddr01
ToAddr02
ToCity
ToState
PostalCode
TOEMail
Description
ReferenceNumber
Weight
Is this series repeated (meaning can you have multiples of this sequence in one file) or is there one set of data per file?
ASKER
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.
ASKER
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";
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";
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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"
"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"
ASKER
Error 1 The name 'shipment' does not exist in the current context
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
one last thing. How would i write the reader.ReadLine line to the console so i could validate the file is inputing correctly.
ASKER
never mind i think i got it. Let me review all this and i will give my final answer in tommorow.
ASKER
it's working. Thanks alot
ASKER
thanks you all are great.
NP. Glad to help :)
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.ReadL
// 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();
}
}
}