Reputation: 17
I am trying to create a desktop application in WPF .NET Framework
and whenever I try to create several text files on load and then write to them, via File.WriteAllLines
- the program lags and then the window closes. It is not showing any exceptions or problems in the output window. Here is the code:
public partial class MainWindow : Window
{
public string FolderPath = @"C:\_Kooper Young FBLA";
public string Person1Path = @"C:\_Kooper Young FBLA\Person1.txt";
public string Person2Path = @"C:\_Kooper Young FBLA\Person2.txt";
public string Person3Path = @"C:\_Kooper Young FBLA\Person3.txt";
public string Person4Path = @"C:\_Kooper Young FBLA\Person4.txt";
public string Person5Path = @"C:\_Kooper Young FBLA\Person5.txt";
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Create Folder
if(!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
else
{
Console.WriteLine("");
}
//Create File 1
if (!File.Exists(Person1Path))
{
File.Create(Person1Path);
string[] DefaultData1 = {"Person 1", "0"};
File.WriteAllLines(Person1Path, DefaultData1);
}
else
{
Console.WriteLine("");
}
//Create File 2
if (!File.Exists(Person2Path))
{
File.Create(Person2Path);
string[] DefaultData2 = { "Person 2", "0" };
File.WriteAllLines(Person2Path, DefaultData2);
}
else
{
Console.WriteLine("");
}
//Create File 3
if (!File.Exists(Person3Path))
{
File.Create(Person3Path);
string[] DefaultData3 = { "Person 3", "0" };
File.WriteAllLines(Person3Path, DefaultData3);
}
else
{
Console.WriteLine("");
}
//Create File 4
if (!File.Exists(Person4Path))
{
File.Create(Person4Path);
string[] DefaultData4 = { "Person 4", "0" };
File.WriteAllLines(Person4Path, DefaultData4);
}
else
{
Console.WriteLine("");
}
//Create File 5
if (!File.Exists(Person5Path))
{
File.Create(Person5Path);
string[] DefaultData5 = { "Person 5", "0" };
File.WriteAllLines(Person5Path, DefaultData5);
}
else
{
Console.WriteLine("");
}
}
Upvotes: 0
Views: 124
Reputation: 254
The problem is actually quite simple.
When you use the File.Create
the file is indeed created, but the lock that is placed on it by File.Create
does not release automatically. This means that when you try to write in the file using File.WriteAllLines
the File is inaccessible, since it's used by another process.
In your case the fix is actually quite simple, you can simply get rid of the File.Create
calls since File.WriteAllLines
will create the file if the file does not exists.
So your code should look like this :
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
else
{
Console.WriteLine("");
}
//Create File 1
if (!File.Exists(Person1Path))
{
string[] DefaultData1 = { "Person 1", "0" };
File.WriteAllLines(Person1Path, DefaultData1);
}
else
{
Console.WriteLine("");
}
//Create File 2
if (!File.Exists(Person2Path))
{
string[] DefaultData2 = { "Person 2", "0" };
File.WriteAllLines(Person2Path, DefaultData2);
}
else
{
Console.WriteLine("");
}
//Create File 3
if (!File.Exists(Person3Path))
{
string[] DefaultData3 = { "Person 3", "0" };
File.WriteAllLines(Person3Path, DefaultData3);
}
else
{
Console.WriteLine("");
}
//Create File 4
if (!File.Exists(Person4Path))
{
string[] DefaultData4 = { "Person 4", "0" };
File.WriteAllLines(Person4Path, DefaultData4);
}
else
{
Console.WriteLine("");
}
//Create File 5
if (!File.Exists(Person5Path))
{
string[] DefaultData5 = { "Person 5", "0" };
File.WriteAllLines(Person5Path, DefaultData5);
}
else
{
Console.WriteLine("");
}
Also as many of the comments points out, there is plenty of way to refactor your code to make it more readable, and less error prone.
Upvotes: 3