我有一个名为firstRun()
,其中我有两个定义的布尔值filesDeleted
and dirsDeleted
.
也在函数里面我有if (filesDeleted == true && dirsDeleted == true) {
当我尝试调试应用程序时出现错误 -Use of unassigned local variable 'filesDeleted'
and 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);
}
}
改变你的
bool filesDeleted;
bool dirsDeleted;
to
bool filesDeleted = false;
bool dirsDeleted = false;
这些是局部变量,它们must在使用它们之前进行分配。
From 5.1.7 Local variables http://msdn.microsoft.com/en-us/library/aa691170%28v=VS.71%29.aspx
局部变量不会自动初始化,因此没有
默认值。为了进行明确的分配检查,
局部变量最初被视为未分配。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)