By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,763 Members | 1,609 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,763 IT Pros & Developers. It's quick & easy.

Writing files with a separate class file

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.