Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Writing files with a separate class file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David - 19 Jul 2005 17:15 GMT
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
Mythran - 19 Jul 2005 17:40 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.