我有一个名为 的函数firstRun()
,在其中我有两个定义的布尔值filesDeleted
和dirsDeleted
.
在功能内部我也有if (filesDeleted == true && dirsDeleted == true) {
当我尝试调试应用程序时出现错误 -Use of unassigned local variable 'filesDeleted'
并Use of unassigned local variable 'dirsDeleted'
尝试了很多不同的解决方案,但根本没有用。
这是代码:
private void firstRun(bool forceDelete) {
string Path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myLauncher");
string[] Files = Directory.GetFiles(Path);
string[] Dirs = Directory.GetDirectories(Path);
bool filesDeleted;
bool dirsDeleted;
if (forceDelete == true)
{
if (Directory.Exists(Path))
{
string lastFile = Files[Files.Length - 1];
foreach (string file in Files)
{
if (file == lastFile)
{
filesDeleted = true;
MessageBox.Show("test");
}
File.Delete(file);
}
string lastDir = Dirs[Dirs.Length - 1];
foreach (string dir in Dirs)
{
if (dir == lastDir)
{
dirsDeleted = true;
MessageBox.Show("test2");
}
Directory.Delete(dir, true);
}
if (filesDeleted == true && dirsDeleted == true)
{
//code when everything deleted
}
}
else
{
Directory.CreateDirectory(Path);
}
}