I am trying to create a program that writes some kind of data to a file in C#. when I tried running the code, the console said that the file is busy even though I am not opening it at all.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace databaseProject
{
public class Program
{
static void Main(string[] args)
{
Functions F = new Functions();
string studentID;
string studentName;
string subjectA;
string subjectB;
Console.WriteLine("Please enter the student's ID.");
studentID = Console.ReadLine();
Console.WriteLine("Please enter the student's name.");
studentName = Console.ReadLine();
Console.WriteLine("Please enter the student's score for subject A");
subjectA = Console.ReadLine();
Console.WriteLine("Please enter the student's score for subject B");
subjectB = Console.ReadLine();
Functions.saveFile(studentID, studentName, subjectA, subjectB);
}
}
public class Functions
{
public static void saveFile(string studentID, string studentName, string subjectA, string subjectB)
{
FileStream stream = new FileStream("DATA.txt", FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
Console.WriteLine(studentID);
Console.WriteLine(studentName);
Console.WriteLine(subjectA);
Console.WriteLine(subjectB);
File.AppendAllText("DATA.txt", studentID + Environment.NewLine);
File.AppendAllText("DATA.txt", studentName + Environment.NewLine);
File.AppendAllText("DATA.txt", subjectA + Environment.NewLine);
File.AppendAllText("DATA.txt", subjectB + Environment.NewLine);
writer.Close();
stream.Close();
}
}
}
the console will write this instead
Unhandled exception. System.IO.IOException: The process cannot access the file 'C:\Users\User\Documents\codes\C#\databaseProjectCSlash\databaseProjectCSlash\bin\Debug\net6.0\DATA.txt' because it is being used by another process.
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
at System.IO.StreamWriter.ValidateArgsAndOpenPath(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.File.AppendAllText(String path, String contents)
at databaseProject.Functions.saveFile(String studentID, String studentName, String subjectA, String subjectB) in C:\Users\User\Documents\codes\C#\databaseProjectCSlash\databaseProjectCSlash\Program.cs:line 52
at databaseProject.Program.Main(String[] args) in C:\Users\User\Documents\codes\C#\databaseProjectCSlash\databaseProjectCSlash\Program.cs:line 34
after sending that message, the file would be wiped out of any data.
Thank you for your help, I really appreciate that.