473,395 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Writing files with a separate class file

Hi all,

I am trying to write a class file, but am not quite sure what I am doing.

What I want to do is from within an ASP page is to call a class.

The class must then create a text file (using StreamWriter).
I then need to pass various information to the class, then eventually close
the file and send it out to the calling page.

I am thinking something like...

namespace MyClass
{
public class writeFile
{
private string FileName = "";
public string MyFile
{
get ...
set ...
}

public StreamWriter WriteFile(StreamWriter myStream)
{
new StreamWriter(MyFile);
}

public void AddText(string MyText)
{
MyFile.Write(MyText);
}

public void AddSignature()
{
MyFile.Write("Dave Colliver");
}

public void CloseFile()
{
MyFile.Close();
}
}
}
I know the above is wrong but it is to give you an idea of what I want. From
the ASPX file, I will need to do something like...

writeFile wf = new writeFile();
writeFile.MyFile = "c:\dave.txt";
writeFile.AddText("Learning to write a class file");
writeFile.AddSignature();
writeFile.Save(MyFilename);
// Thinking about the above, I won't need to set MyFile value, but I think
you get the picture...

Can someone point me in the right direction. I have tried googling but
unfortunately, I can't get my search query fine tuned enough to return a
decent result.

Thanks for your help.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Nov 19 '05 #1
4 1265

"David" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:eN**************@TK2MSFTNGP14.phx.gbl...
Hi all,

I am trying to write a class file, but am not quite sure what I am doing.

What I want to do is from within an ASP page is to call a class.

The class must then create a text file (using StreamWriter).
I then need to pass various information to the class, then eventually
close the file and send it out to the calling page.


namespace MyNameSpace
{
public class MyClass
{
private string mFileName = String.Empty;
private System.Text.StringBuilder mContents = null;

public MyClass() : base()
{
mContents = new System.Text.StringBuilder();
}

public string FileName
{
get {
return mFileName;
}
set {
mFileName = value;
}
}

public string Contents
{
get {
return mContents.ToString();
}
set {
mContents = new System.Text.StringBuilder(value);
}
}

public void SaveFile()
{
StreamWriter sw = System.IO.File.CreateText(this.FileName);

try {
sw.Write(this.Contents);
} finally {
// Cleanup.
sw.Close();
}
}

public overloads void AddText(string Text)
{
mContents.Append(Text);
}

public overloads void AddText(string Format, params object[] Args)
{
mContents.AppendFormat(Format, Args);
}

public void AddSignature()
{
this.AddText("Dave Colliver");
}
}
}
To use:

MyClass mc = new MyClass();
mc.FileName = "C:\\dave.txt";
mc.AddText("Learning to write a class file.");
mc.AddSignature();
mc.SaveFile();

Using the above code (off top of head), the file itself is not opened until
the SaveFile method is called. This prevents the file from being locked or
having an open handle when you don't really need it (not that it would be
locked as the file it is being "Created", but you should get the idea).

HTH,
Mythran

Nov 19 '05 #2
You are a star, thank you.

I will give it a go and let you know.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

"David" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:eN**************@TK2MSFTNGP14.phx.gbl...
Hi all,

I am trying to write a class file, but am not quite sure what I am doing.

What I want to do is from within an ASP page is to call a class.

The class must then create a text file (using StreamWriter).
I then need to pass various information to the class, then eventually
close the file and send it out to the calling page.


namespace MyNameSpace
{
public class MyClass
{
private string mFileName = String.Empty;
private System.Text.StringBuilder mContents = null;

public MyClass() : base()
{
mContents = new System.Text.StringBuilder();
}

public string FileName
{
get {
return mFileName;
}
set {
mFileName = value;
}
}

public string Contents
{
get {
return mContents.ToString();
}
set {
mContents = new System.Text.StringBuilder(value);
}
}

public void SaveFile()
{
StreamWriter sw = System.IO.File.CreateText(this.FileName);

try {
sw.Write(this.Contents);
} finally {
// Cleanup.
sw.Close();
}
}

public overloads void AddText(string Text)
{
mContents.Append(Text);
}

public overloads void AddText(string Format, params object[] Args)
{
mContents.AppendFormat(Format, Args);
}

public void AddSignature()
{
this.AddText("Dave Colliver");
}
}
}
To use:

MyClass mc = new MyClass();
mc.FileName = "C:\\dave.txt";
mc.AddText("Learning to write a class file.");
mc.AddSignature();
mc.SaveFile();

Using the above code (off top of head), the file itself is not opened
until the SaveFile method is called. This prevents the file from being
locked or having an open handle when you don't really need it (not that it
would be locked as the file it is being "Created", but you should get the
idea).

HTH,
Mythran

Nov 19 '05 #3
Works beautifully, thank you very much.

I am assuming that once the class is written the way I want it, it can be
used in desktop apps as well as web apps?

Something else I am thinking about is to automatically download the file (a
web file). How easy would it be to modify the save function to start a
download?

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

"David" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:eN**************@TK2MSFTNGP14.phx.gbl...
Hi all,

I am trying to write a class file, but am not quite sure what I am doing.

What I want to do is from within an ASP page is to call a class.

The class must then create a text file (using StreamWriter).
I then need to pass various information to the class, then eventually
close the file and send it out to the calling page.


namespace MyNameSpace
{
public class MyClass
{
private string mFileName = String.Empty;
private System.Text.StringBuilder mContents = null;

public MyClass() : base()
{
mContents = new System.Text.StringBuilder();
}

public string FileName
{
get {
return mFileName;
}
set {
mFileName = value;
}
}

public string Contents
{
get {
return mContents.ToString();
}
set {
mContents = new System.Text.StringBuilder(value);
}
}

public void SaveFile()
{
StreamWriter sw = System.IO.File.CreateText(this.FileName);

try {
sw.Write(this.Contents);
} finally {
// Cleanup.
sw.Close();
}
}

public overloads void AddText(string Text)
{
mContents.Append(Text);
}

public overloads void AddText(string Format, params object[] Args)
{
mContents.AppendFormat(Format, Args);
}

public void AddSignature()
{
this.AddText("Dave Colliver");
}
}
}
To use:

MyClass mc = new MyClass();
mc.FileName = "C:\\dave.txt";
mc.AddText("Learning to write a class file.");
mc.AddSignature();
mc.SaveFile();

Using the above code (off top of head), the file itself is not opened
until the SaveFile method is called. This prevents the file from being
locked or having an open handle when you don't really need it (not that it
would be locked as the file it is being "Created", but you should get the
idea).

HTH,
Mythran

Nov 19 '05 #4

"David" <da*****************@revilloc.REMOVETHIS.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Works beautifully, thank you very much.

I am assuming that once the class is written the way I want it, it can be
used in desktop apps as well as web apps?

Something else I am thinking about is to automatically download the file
(a web file). How easy would it be to modify the save function to start a
download?


public void SaveFile()
{
StreamWriter sw = System.IO.File.CreateText(this.FileName);

try {
sw.Write(this.Contents);
} finally {
// Cleanup.
sw.Close();
}
}

In addition to the SaveFile method, you would use another method to send the
file to the client...such as the following:

public void SendFile(bool OpenSaveDialog)
{
HttpResponse response = HttpContext.Current.Response;
byte[] bytes = ASCIIEncoding.ASCII.GetBytes(this.Contents);

// Setup the response headers.
response.Clear();
response.ClearHeaders();
if (OpenSaveDialog) {
response.AddHeader("content-type", "text/plain");
response.AddHeader(
"content-disposition",
"attachment;filename=\"output.txt\""
);
}

// Send the file to the client.
try {
response.BinaryWrite(bytes);
} catch (Exception ex) {
// May want to customize exception handling.
response.Clear();
response.ClearHeaders();
response.Write(ex.ToString());
} finally {
response.End();
}
}

This will either prompt the user to open or save (if OpenSaveDialog is true)
or display the text file in the browser (if OpenSaveDialog is false).
Either way, I think it's what you want ;)

HTH,
Mythran

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
6
by: msnews.microsoft.com | last post by:
Hi all, I was trying to find an easy way to write in the standard configuration (like app.config) xml-based files. I found classes that can be used to read this information (in...
1
by: Hans De Schrijver | last post by:
I'm new to C# development, so here's a basic question. I'm developing a code library (no UI) where several of the classes will implement an IPersistable interface. Each class lives in its own...
24
by: ypjofficial | last post by:
Hello all, I have written a class with many private data members.and i am putting it in a separate dll file. Now when i link that file while writing my main program module,natuarally i have to...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: xz | last post by:
What sense do h files make in c/cpp? I never thought about this question before. Does the existence of h files make sense at all? Why do we need to declare functions in h files and...
1
by: lqnt1981 | last post by:
Following is the code in python that opens all files under a directories and puts a comma in place of whitespace, I can see the output on the command line, but I am not sure what to do in order to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.