Solved

Write read data to file using network stream

Posted on 2004-09-02
6
368 Views
Last Modified: 2008-01-09
Hello;
My question is that, i read datas through network stream. (Using NNTP) What i need to learn is how do i create the file that has been read? I get datas into string builder, what sould i do to create the actual file ?
0
Comment
Question by:lexicon_dominus
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:armoghan
ID: 11970577
StringBuilder bu = "hello";

FileInfo t = new FileInfo("Arungg.txt");
    StreamWriter Tex =f.CreateText();
    Tex.WriteLine(bu.ToString());
    Tex.close;
    Console.WriteLine(" The Text file named Arungg is created ");
0
 
LVL 4

Author Comment

by:lexicon_dominus
ID: 11972478
Thank armoghan but i wasn't clear i think. The files are not text files they are .rar, .zip, .exe etc files. I know the file types before reading the datas.
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11973007
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 18

Expert Comment

by:armoghan
ID: 11973036
0
 
LVL 2

Accepted Solution

by:
sholodak earned 500 total points
ID: 11973267
If you are retrieving files off of a newsgroup and populating a StringBuilder, I'm assuming that these files are encoded with UUEncode/Base64/yEnc or something along those lines.  These algorithms allow for converting binary <--> ascii.

Have a look at this article:
http://www.codeproject.com/dotnet/TextCoDec.asp

The class is written in VB.NET, but if your not too concerned with whats under the hood, I'd just include a reference to the DLL and call the appropriate functions.  It can take care of encoding/decoding to/from files/strings.  

In your example, you would call something along the lines of:

DecodedFile[] newfiles = yEnc.Decode(stringbuilder.ToString(), @"C:\directory\for\files", 0);

Some of the other encoders/decoders don't take strings as inputs, only streams.  You could get around this by doing something like this:

string BuiltString = sb.ToString();
byte[] StringAsBytes = System.Text.Encoding.ASCII.GetBytes(BuiltString);
System.IO.MemoryStream ms = new System.IO.MemoryStream(StringAsBytes);
uue.Decode(ms, @"C:\directory\for\files");

Once the files are decoded, they should become RAR's and ZIP's again.

One other thing.  If you're dealing with Base64 encoding, .NET has functions to take care of this for you:
string encoded = Convert.ToBase64String(binaryBytes);
byte[] decoded = Convert.FromBase64String(stringVersion);

You might want to compare the performance of this with the one in the library above.  The library might just wrap around the .NET function.

-- One other note.  If you're on 2000 or XP with NTFS, the person running your program must have Write permission to the target directory.  If this is a web app, that would probably be IUSR_MACHINENAME.

Scott
0
 
LVL 4

Author Comment

by:lexicon_dominus
ID: 11974157
Thank you sholodak, that was what i was looking for...
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now