1

I have the following problem: i have a string variable, which has to store a filepath. In a foreach loop i go through all files in a certain directory and im looking for the oldest, which is saved in that string variable. When the loop is finished i try to delete that file, but i get an error: Use of an unassigned local variable.

Here is the code:

DateTime min = DateTime.Now;
string[] fileNames = Directory.GetFiles(somePath);
string fileDelete;
int countFiles = fileNames.Length;
if (countfiles > 5)
{
    foreach (string someFile in fileNames)
    {
        FileInfo infoFile = new FileInfo(someFile);
        if (infoFile.CreationTime <= min)
        {
            min = infoFile.CreationTime;
            fileDelete = someFile;
        }   
    }
    File.Delete(fileDelete);
}   

it says that the string fileDelete in File.Delete(fileDelete) has no value, but the fun thin is, when i give it a value at the beginning just like that:

string fileDelete = "blabla";

it works perfectly fine. This is just a snipped of the method in case you are wondering

3
  • 1
    When you do string fileDelete = "blabla";, the variable is no longer unassigned. Is countFiles bigger than 5? Does at least one file have a creation time before DateTime.Now? Your code does not guarantee that fileDelete is assigned, so it's going to go wrong at some point. Commented Jan 20, 2016 at 12:04
  • As others said, compiler can only work with variable when it is initialized explicitly. So just use string fileDelete = ""; Commented Jan 20, 2016 at 13:00
  • Thank you all for your awnsers. I understand the problem now. I just forgot that the compiler can not know that condition 2 is always met when condition 1 is (in my programm), and the compiler saw a case, where File.Delete will be executed without a value, even though this can not happen in my code. Commented Jan 20, 2016 at 13:44

5 Answers 5

3

in C# you have to initialise variables, otherwise you could pass garbage values to File.Delete.

I recommend using

string fileDelete = null; // or ""

and later check for that.

if (!string.IsNullOrEmpty(fileDelete))
{
  File.Delete(fileDelete);
}
Sign up to request clarification or add additional context in comments.

Comments

1

It works exactly as intended.

In C# local variables are not initialised automatically at declaration.

You're not assinging any value to the fileDelete when declaring it and assigning it only under some condition in your loop.

But you're trying to use its value outside this condition in loop, thus compiler can't deduce - will fileDelete has some value at runtime or not (if code under condition will not be executed - then fileDelete will has no value).

Thus compiler generates this error.

1 Comment

thats right thank you. I just forgot that the compiler can not know that condition 2 is always met when condition 1 is (in my programm).
0

Imagine this scenario:

  • countFiles > 5 is True
  • infoFile for someFile is bigger than min

What value will fileDelete have when passed to File.Delete?

Answer: In such scenario fileDelete will be uninitialized, thus the error message.

1 Comment

Thats 100% right. I just didn't see that case because it can not happen in my program, but of course the compiler thinks it could happen.
0

You must initialize your variable. see at Why compile error "Use of unassigned local variable"?

Comments

0

Use this

DateTime min = DateTime.Now;
string[] fileNames = Directory.GetFiles(somePath);
string fileDelete;
int countFiles = fileNames.Length;
if (countfiles > 5)
{
    foreach (string someFile in fileNames)
    {
        FileInfo infoFile = new FileInfo(someFile);
        if (infoFile.CreationTime <= min)
        {
            min = infoFile.CreationTime;
            fileDelete = someFile;
            File.Delete(fileDelete);
        }   
    }

}   

2 Comments

this would make fileDelete redundant completely
I didn't test this but i think this will just delete all files that are older than now. But i wanted to delete only the oldest file in a certain directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.