> Hi all,
>
[quoted text clipped - 5 lines]
> 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
David - 19 Jul 2005 18:03 GMT
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
>> Hi all,
>>
[quoted text clipped - 83 lines]
> HTH,
> Mythran
David - 19 Jul 2005 20:19 GMT
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
>> Hi all,
>>
[quoted text clipped - 83 lines]
> HTH,
> Mythran
Mythran - 19 Jul 2005 21:27 GMT
> Works beautifully, thank you very much.
>
[quoted text clipped - 4 lines]
> (a web file). How easy would it be to modify the save function to start a
> download?
>> public void SaveFile()
>> {
[quoted text clipped - 7 lines]
>> }
>> }
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