Open File
-
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
-
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
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
-
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
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.
-
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.
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.
-
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.