Writing files with a separate class file

D

David

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
 
M

Mythran

David said:
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
 
D

David

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 said:
David said:
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
 
D

David

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 said:
David said:
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
 
M

Mythran

David said:
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?


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top