Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Open File

Open File

Scheduled Pinned Locked Moved ASP.NET
helpcsharp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Terick
    wrote on last edited by
    #1

    Hi, I have two Forms in my web application using c#. Form1: Opens a folder and checks for latest file (ex: Text1.txt) Increments the number by 1 and creates a new text file with new number (ex: Text2.txt) Form2: Opens new file (ex Text2.txt) and reads line by line and writes to Form Every time the page is submitted, a new text file is created, so I made the number assigned to the text file an integer and assigned the file name using the following code:

    StreamWriter sw;
    sw=File.CreateText(@"C:\Desktop\Text" +x +".txt");
    sw.Close();

    I am having a problem passing the exact file name from Form 1 to Form 2. I want the file to be created on the button click in form 1 but it must be open and read in the timer of form2. Please help if you can, especially with code examples. I'm completely stuck! Thanks

    N R 2 Replies Last reply
    0
    • T Terick

      Hi, I have two Forms in my web application using c#. Form1: Opens a folder and checks for latest file (ex: Text1.txt) Increments the number by 1 and creates a new text file with new number (ex: Text2.txt) Form2: Opens new file (ex Text2.txt) and reads line by line and writes to Form Every time the page is submitted, a new text file is created, so I made the number assigned to the text file an integer and assigned the file name using the following code:

      StreamWriter sw;
      sw=File.CreateText(@"C:\Desktop\Text" +x +".txt");
      sw.Close();

      I am having a problem passing the exact file name from Form 1 to Form 2. I want the file to be created on the button click in form 1 but it must be open and read in the timer of form2. Please help if you can, especially with code examples. I'm completely stuck! Thanks

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      What exactly are you having difficulties with? What have you tried.

      Terick wrote:

      especially with code examples

      The people most likely to give you the proper answer will not give you the code without seeing that you are trying yourself first.


      only two letters away from being an asset

      1 Reply Last reply
      0
      • T Terick

        Hi, I have two Forms in my web application using c#. Form1: Opens a folder and checks for latest file (ex: Text1.txt) Increments the number by 1 and creates a new text file with new number (ex: Text2.txt) Form2: Opens new file (ex Text2.txt) and reads line by line and writes to Form Every time the page is submitted, a new text file is created, so I made the number assigned to the text file an integer and assigned the file name using the following code:

        StreamWriter sw;
        sw=File.CreateText(@"C:\Desktop\Text" +x +".txt");
        sw.Close();

        I am having a problem passing the exact file name from Form 1 to Form 2. I want the file to be created on the button click in form 1 but it must be open and read in the timer of form2. Please help if you can, especially with code examples. I'm completely stuck! Thanks

        R Offline
        R Offline
        Rutvik Dave
        wrote on last edited by
        #3

        if this is a web application then why you are using c:\ .... it should be ~\\Files\\xxx. to pass data between web forms there are many ways. but you can use Session. in Form 1 at the Button Click.

        StreamWriter sw;
        string TextFile = @"C:\Desktop\Text" +x +".txt" // if x is int then use x.ToString()
        sw=File.CreateText(TextFile);
        StreamWriter sw;

        Session["TextFile"] = TextFile;

        and in Form 2 Page_Load

        string TextFile = (string) Session["TextFile"];
        // now use the TextFile variable to read the file using StreamReader r = File.OpenText(TextFile);

        but still you need to read more about Session and File Handling.

        T 2 Replies Last reply
        0
        • R Rutvik Dave

          if this is a web application then why you are using c:\ .... it should be ~\\Files\\xxx. to pass data between web forms there are many ways. but you can use Session. in Form 1 at the Button Click.

          StreamWriter sw;
          string TextFile = @"C:\Desktop\Text" +x +".txt" // if x is int then use x.ToString()
          sw=File.CreateText(TextFile);
          StreamWriter sw;

          Session["TextFile"] = TextFile;

          and in Form 2 Page_Load

          string TextFile = (string) Session["TextFile"];
          // now use the TextFile variable to read the file using StreamReader r = File.OpenText(TextFile);

          but still you need to read more about Session and File Handling.

          T Offline
          T Offline
          Terick
          wrote on last edited by
          #4

          I'm using C:\ because I'm running this on the localhost and the file is shared on my network. The text file is then picked up by a program on another machine on the network. This program will make changes that my machine reads out in form 2. Is there a better way to save this text file that can still achieve these results? I'm not familiar with ~\\Files\\xxxx Thank you for your advice on the session state. I will try that.

          1 Reply Last reply
          0
          • R Rutvik Dave

            if this is a web application then why you are using c:\ .... it should be ~\\Files\\xxx. to pass data between web forms there are many ways. but you can use Session. in Form 1 at the Button Click.

            StreamWriter sw;
            string TextFile = @"C:\Desktop\Text" +x +".txt" // if x is int then use x.ToString()
            sw=File.CreateText(TextFile);
            StreamWriter sw;

            Session["TextFile"] = TextFile;

            and in Form 2 Page_Load

            string TextFile = (string) Session["TextFile"];
            // now use the TextFile variable to read the file using StreamReader r = File.OpenText(TextFile);

            but still you need to read more about Session and File Handling.

            T Offline
            T Offline
            Terick
            wrote on last edited by
            #5

            Rutvik, Thank you for your suggestion of using Session. This worked exactly as I hoped. The only thing I changed was that I placed the text from Form2 in my timer_tick and it worked. Thanks again!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups