如何使foreach循环中的变量存在?

时间:2017-01-02 19:48:37

标签: c# string variables foreach

如果这听起来像一个无知的问题我很抱歉,但我对C#很新。我有一个错误,让我的程序工作:

"名称' filesInfo'在当前上下文中不存在"

我试图让这个变量存在,并尝试通过在foreach循环之外声明它来解决这个问题,但无济于事。

这是我目前的剧本:

var parser = new HtmlParser();
var doc = parser.Parse("http://localhost/test.html");
var element = doc.QuerySelector("span.foo");

3 个答案:

答案 0 :(得分:4)

您在filesInfo循环中声明foreach。因此,其范围仅限于该循环。如果要在其他地方使用它,则必须将其移动到相应的范围:

FileInfo filesInfo;

foreach (string s in files)
{
    string fileName = Path.GetFileName(s);
    filesInfo = new FileInfo(fileName);
}

try
{
    if (filesInfo.LastWriteTime.Date == DateTime.Today.Date)
    {
        File.Move(src, dst);
        Console.WriteLine("Modified files in {0} were moved to {1}", src, dst);
    }
    else
    {
        Console.WriteLine("No new or modified files were created today.");
    }
}
catch (Exception e)
{
    Console.WriteLine("The process failed: {0}", e.ToString());
}

话虽如此,您目前正在循环的每次迭代中覆盖filesInfo。我想你想检查filesInfo s的每个值,所以你真正想做的是:

foreach (string s in files)
{
    string fileName = Path.GetFileName(s);
    FileInfo filesInfo = new FileInfo(fileName);

    try
    {
        if (filesInfo.LastWriteTime.Date == DateTime.Today.Date)
        {
            File.Move(src, dst);
            Console.WriteLine("Modified files in {0} were moved to {1}", src, dst);
        }
        else
        {
            Console.WriteLine("No new or modified files were created today.");
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("The process failed: {0}", e.ToString());
    }
}

答案 1 :(得分:2)

试试这个:

public void Move_Modified_Files()
{
    foreach (string s in files)
    {
        string fileName = Path.GetFileName(s);
        FileInfo filesInfo = new FileInfo(fileName);

        try
        {
            if (filesInfo.LastWriteTime.Date == DateTime.Today.Date)
            {
                File.Move(src, dst);
                Console.WriteLine("Modified files in {0} were moved to {1}", src, dst);
            }
            else
            {
                Console.WriteLine("No new or modified files were created today.");
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

答案 2 :(得分:1)

你宣布了

FileInfo filesInfo = new FileInfo(fileName);

在第一个循环内。当此上下文以}结尾时,变量将消失。

我的猜测是你想要这样做。

public class MyClass
{
    public static string src = @"C:\Users\Bold Defiance\Desktop\FolderA";
    public static string dst = @"C:\Users\Bold Defiance\Desktop\FolderB";
    public static string[] files = System.IO.Directory.GetFiles(src, "*.txt");

    public void Move_Modified_Files()
    {
        foreach (string s in files)
        {
            // These values will exist until their enclosing context is closed
            // The context starts with the most recent opening bracket {
            // So these values, will exist until this loop iterates to the next value
            string fileName = Path.GetFileName(s);
            FileInfo filesInfo = new FileInfo(fileName);

            // Attempt to use the currently selected fileinfo
            try
            {
                if (filesInfo.LastWriteTime.Date == DateTime.Today.Date)
                {
                    File.Move(src, dst);
                    Console.WriteLine("Modified files in {0} were moved to {1}", src, dst);
                }
                else
                {
                    Console.WriteLine("No new or modified files were created today.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }

            // The next } bracket below is the one that closes the context mentioned earlier
            // When it closes, all values declared in this sub-context will no longer exist.
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        MyClass cls = new MyClass();
        cls.Move_Modified_Files();
    }
}