Custom text file name

Joined
Oct 25, 2014
Messages
12
Programming Experience
Beginner
Hello
i know that the code below can be used to creat and update a text file but is there a possibility that the path to the file can be saved as a string.


C#:
                {
                    string[] lineROLL = { "\nROLL NUMBER\n", rollnumber1.Text };
                    File.AppendAllLines(@".\file.txt"", lineROLL);
                }

what i need is that i want make multiples text files each with a different number to store different sorts of strings.

further explaining:


i need the data path to be updated after a particular interval for example when i press the update button the path of file should be c://"filename_""rollnumber"


you can check out the my project at

http://www.csharpforums.net/c-general-discussion/2325-school-registration-system.html

Thanks
 
I looked at your other post, where you showed the windows form you use. Not sure what you want to name the text file after, but for the sake of the example, let's use the students name. What you will need are the control names for the student's name and the roll number, from your form. For this example, I will use textName and textRoll as an example.

string fileName = textName.text;
string[] rollNumber = {"\nROLL NUMBER\n" + textRoll.text}; //not sure why your using a array here, and populating it with one value. Would be better in a foreach statement. But for your example I stuck with it.

then when you do your file command use those variables instead of literal strings.

File.AppendAllLines(fileName, rollNumber);

I'm new to c# but I think that is what you were asking.
 
i need the data path to be updated after a particular interval for example when i press the update button the path of file should be c://"filename_""rollnumber"
Thanks
If I understand you correctly, you want the filenames to be dynamic.

Do a search for C# string concatenation. Some examples that you can use

C#:
string filename = "basefilename_" + rollnumber + ".txt";
or
string filename = String.Format("{0}_{1}{2}", basefilename, rollnumber, extension);

And next you can use filename as the first argument in File.AppendAllLines().
 

Latest posts

Back
Top Bottom