Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to codeing to create text file with different id on button click.Whenever i click on button it will create a text file with a a name but file name is different then last created file.After that i will write somthing and save it and also retrive data on web page.
Posted
Updated 19-Oct-11 23:17pm
v3

1) You can use Guid as your file name.

2) You can keep a counter and increment that for each file saved.
 
Share this answer
 
Comments
RaisKazi 20-Oct-11 7:11am    
My 5! I will go with Guid option.
Try this
string FileName=Test+DateTime.Now.ToString().Replace("/", "").Replace(".", "").Replace(":", "").Replace(" ", "")+".txt";
string text = "Test string to write inside the text file";
System.IO.File.WriteAllText(@FileName, text);
 
Share this answer
 
Comments
BobJanova 20-Oct-11 7:03am    
My 4 - will work as long as files are not saved more than once per second, and the system clock never turns back (might be a problem at the end of daylight savings, for example).
Concatenate file with FileName + HH:MM:SS
fileName024658

Try this
C#
string path = @"D:\";
string fileName = "TestFile" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + ".txt";
File.Create(path + fileName);
 
Share this answer
 
v2
There are number of ways to solve this problem as:

1. You can use and variable appended with file name that will make it unique.

2. If you want to implement it with counter then you should save it in database or a file because when application will down all values of counter will automatically rest.
 
Share this answer
 
As suggested by Mehdi use Guid. Try as below code.
C#
string myText = "Your Text";
string myFile = System.Guid.NewGuid().ToString() + ".txt";
System.IO.File.WriteAllText(myFile, myText);


Total concept of Guid is based on "Globally unique identifier".
http://en.wikipedia.org/wiki/Guid

Updated -
To read your saved text files, have a look at below links.
http://www.gridview.net/read-text-file-in-c-asp-net/
http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx
http://msdn.microsoft.com/en-us/library/aa287534(v=vs.71).aspx

Also don't forget Google is your Friend.
Google Search Result - To read text from File
 
Share this answer
 
v4
Comments
Janardan Pandey 20-Oct-11 7:20am    
Please give me details using short program using c# dot net
RaisKazi 20-Oct-11 7:32am    
Above code is in C#. Please refer my updated answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900