Unable to write text to a .txt file

Multi tool use
Unable to write text to a .txt file
I have a Windows forms application, that is supposed to store its config data on a file called config.txt
. I have this code, which runs when the form is initialized. Here is the function:
config.txt
string folderPath = @‘C:UsersPublicPeriodic_Clock’;
string filePath = @‘C:UsersPublicPeriodic_Clockconfig.txt’;
public Form1()
{
InitializeComponent();
once = 0;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (!File.Exists(filePath))
{
new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
File.WriteAllText(filePath, "Alarm" + Environment.NewLine + "Alarm Ringing!" + Environment.NewLine + 1);
}
else
{
}
notificationTitle = File.ReadLines(filePath).Skip(0).Take(1).First();
notificationText = File.ReadLines(filePath).Skip(1).Take(1).First();
}
Pretty much, I have an app that sends a notification when a timer goes off. I want to use this config file to store a custom title, text and notification type for the notification. (so that the user can customize what the notification looks like) My intention with this was to have the first line as the notification title, second line as the text, and the third line as a number from 0 - 3, to store the type of notification (info, error, etc.)
I can not manage to get the program to write anything to the text file. It creates it, but it seems to completely skip the part where I use File.WriteAllText()
. I used other methods like StreamWriter
, but nothing was written to the text document. The code right now skips the WriteAllText
part and creates an error at File.ReadLines
, which just tells me that the sequence contains no elements. What's the problem here? How do I fix it?
File.WriteAllText()
StreamWriter
WriteAllText
File.ReadLines
Why would you open the file and read all lines twice?
– PepitoSh
Jun 30 at 4:48
You have simply misunderstood the use of File.WriteAllText, and also the other calls. Read the documentation. I suggest you use File.WriteAllLines and File.ReadAllLines, and no other calls. If using FileStream, you should use "using (..." and so on.
– Bent Tranberg
Jun 30 at 5:05
You ask what the problem is, so I can tell you that your
new FileStream
will open the file for writing, but then WriteAllText
will also try to do the same. You can't have two simultaneous writes going on. Also, you mention an error, but give no details.– Bent Tranberg
Jun 30 at 5:14
new FileStream
WriteAllText
2 Answers
2
In the end, I ended up with this code:
public Form1()
{
InitializeComponent();
once = 0;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
try
{
FileStream config = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
config.Close();
StreamWriter writetext = new StreamWriter(filePath);
writetext.Write("Alarm");
writetext.WriteLine();
writetext.Write("Alarm Ringing!");
writetext.WriteLine();
writetext.Write("1");
writetext.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error!");
}
notificationTitle = File.ReadLines(filePath).Skip(0).Take(1).First();
notificationText = File.ReadLines(filePath).Skip(1).Take(1).First();
}
edit
In this case, the file is created, but not the content, so my answer doesn't apply. I'll leave it here despite the negative votes, because it may be the solution for others experiencing similar problems.
You need administrator rights to write to the default user profile. You can request them by adding requestedexecutionlevel to the manifest.
Also, do all the other stuff others recommend. A better structured program with better error handling helps you diagnose problems like this faster.
Ive fixed the downvote - obviously I didn't do that. What do you mean String has an addition operator? 1 is an
int
yet it's required to be a string when it's concatenated to be passed to the method which expects a string parameter.– Jeremy Thompson
Jun 30 at 5:47
int
To delete on Android, look for the button under the question and Open in Browser.
– Jeremy Thompson
Jun 30 at 5:48
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Remove the FileStream construction. Also, is this the actual code or do you have a try/catch block in your code as well?
– Lasse Vågsæther Karlsen
Jun 30 at 4:38