Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using Command-based (console application) I want to create a text file and name it as the user name entered by the user, for example if the user enter the user name (Ali) the text file will be created Ali.txt, how can I do that


C#
Console.Write("Username: ");
                string Username = (Console.ReadLine());
                string fileName = Username+ ".txt";
                        string path = System.IO.Path.Combine(@"E:\\", fileName);
                         using (StreamWriter sw = File.CreateText(path))
Posted
Comments
Maciej Los 31-Oct-15 18:01pm    
What's the problem?
R.M49 31-Oct-15 18:11pm    
It give me error in this line string fileName = Username+ ".txt"; for the username
Maciej Los 31-Oct-15 18:19pm    
Replace @"E:\\" with @"E:\"
BillWoodruff 31-Oct-15 18:08pm    
You are close to a solution: read the documentation on StreamReader
Richard Deeming 2-Nov-15 11:11am    
One other thing to consider: there are several characters which are not allowed in Windows file names. What happens if the user types one of those characters? Currently, you code will either crash, or create the file in an unexpected place.

You might want to check the entered string against the list of characters returned from the Path.GetInvalidFileNameChars[^] method, and warn the user if they have entered an invalid character.

Please, read all comments to the question.
Why do i recommend to change @"E:\\" to @"E:\"? Please, see: 2.4.4.5 String literals[^]

Have a look here:
How to: Write to a Text File (C# Programming Guide)[^]
How to: Write Text to a File[^]

C#
Console.Write("Enter username: ");
string Username = Console.ReadLine();
//here you should check if UserName is not null or empty!
string fileName = Username+ ".txt";
string path = System.IO.Path.Combine(@"E:\", fileName);
using (StreamWriter sw = new StreamWriter(path))
{
        sw.WriteLine(Username);
}
 
Share this answer
 
v3
Comments
BillWoodruff 31-Oct-15 18:45pm    
Interesting, it appears the user is asking how to create a file with a user-defined name, rather than write a line of text to the file. My guess is you know something about this user from another question, and realize what he really wants :)
Maciej Los 31-Oct-15 18:49pm    
Bill, take a look at OP's code: fileName = UserName + ".txt";
So, it takes the name entered via user.
BillWoodruff 1-Nov-15 0:46am    
I have no problem that you went "above and beyond" what the OP asked for :) My concern is that the OP may not be clear about the basics of using Files, and you can create a File directly without using a xxxWriter. I'll post what I hope will add to the OP's knowledge.
Maciej Los 1-Nov-15 12:01pm    
Ohhh... OK
BillWoodruff 2-Nov-15 12:09pm    
+5
To add to what Maciej has shown you here:

There are several ways to create a "blank" File can be created in C#:

1.
C#
File.Create(pathName); // will leave the created File open
Since leaving the File open is often not desired:
C#
using (File.Create(pathName))
using (File.Create(pathName)) {}
File.Create(pathName).Close();
File.Create(pathName).Dispose();
2. To write content to a file, with the file automatically created if it does not exist:

See Maciej's answer here ... and ... study the various "Writers" available in the System.IO library: TextWriter, XmlWriter, as well as StreamWriter.
C#
File.WriteAllText(pathName, String.Empty);
File.CreateText(pathName).Close();
Since you may want to do something differently if a File already exists, this is a typical code pattern:
C#
if (File.Exists(pathName))
{
   // whatever if file exists
   // for example 
   // File.AppendAllText(pathName, "text to append");
}
else
{
   // whatver if file does not exist
   // for example 
   // File.WriteAllText(pathName, "initial text");
}
 
Share this answer
 
Comments
R.M49 1-Nov-15 2:29am    
but my question is different from what you all said, I know how to create a textfille and how to write inside it, I want only to know how to choose the name of this file as the input username from the user, why it gives me error in username variable
R.M49 1-Nov-15 2:32am    
Actually I found the solution now, thank you for your effort
BillWoodruff 1-Nov-15 10:13am    
Glad you found a solution !
Maciej Los 1-Nov-15 12:05pm    
OK. Is there any answer which was helpful? If yes, mark it as a solution.
Maciej Los 2-Nov-15 12:37pm    
+5

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