从另一个方法访问变量 [英] Access a variable from another method

查看:84
本文介绍了从另一个方法访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从其他方法访问FileSystemWatcher。



I am having trouble accessing a FileSystemWatcher from another method.

public static void watch(string path)
{
    FileSystemWatcher watcher = new FileSystemWatcher();

    watcher.Path = path;
    watcher.NotifyFilter = NotifyFilters.Attributes |
                   NotifyFilters.CreationTime |
                   NotifyFilters.DirectoryName |
                   NotifyFilters.FileName |
                   NotifyFilters.LastAccess |
                   NotifyFilters.LastWrite |
                   NotifyFilters.Security |
                   NotifyFilters.Size;
    watcher.Filter = "*.*";
    watcher.IncludeSubdirectories = true;
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.Deleted += new FileSystemEventHandler(OnChanged);
    watcher.Renamed += new RenamedEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
}

private static void OnChanged(object sender, FileSystemEventArgs e)
{
    // The reason for having this try block (and EnableRaisingEvents toggling)
    // is to solve a known problem with FileSystemWatcher, in which the event
    // is fired twice.

    try
    {
        MessageBox.Show("File changed.");

        watcher.EnableRaisingEvents = false; // Error: The name 'watcher' does
                                            // not exist in the current context
    }
    finally
    {
        watcher.EnableRaisingEvents = true; // Error: The name 'watcher' does
                                           // not exist in the current context
    }
}





我想更改EnableRaisingEvents属性。



由于范围问题我无法做到这一点。通常我会做的是在一个范围更大的地方声明FileSystemWatcher,但我不能在这里做,因为每次运行方法时都必须创建一个新的。



那么,如何从不同的方法更改对象的属性?



PS:我已经搜索了周围,尝试了不同的东西,但最终没有任何效果。



编辑:只是为了澄清,我必须保留FileSystemWatcher声明INSIDE方法(而不是给它一个更大的范围,从而允许通过另一种方法进行修改)。原因是我每次运行方法时都需要创建一个新的。



I want to change the EnableRaisingEvents property.

I can't do that because of the scope problem. Normally what I would do is declare the FileSystemWatcher somewhere with a bigger scope, but I can't do that here because a new one must be created every time I run the method.

So, how can I change the property of an object from a different method?

PS: I have searched around, and tried different things but ultimately nothing worked.

Just to clarify, I MUST keep the FileSystemWatcher declaration INSIDE the method (as opposed to giving it a bigger scope, thus allowing it to be modified by another method). The reason for this is that I need a new one to be created every time I run the method.

推荐答案

我很确定<在 watch 方法中创建的code> FileSystemWatcher 将是 sender OnChanged 方法的参数。

在这种情况下,它应该非常简单:

I'm pretty sure that the FileSystemWatcher created in the watch method will be the value of the sender parameter of the OnChanged method.
In that case, it should be pretty simple:
private static void OnChanged(object sender, FileSystemEventArgs e)
{
    FileSystemWatcher watcher = sender as FileSystemWatcher;
    if (watcher == null)      // sender isn't a FileSystemWatcher
      return;                 // quit while we're behind! :-)

    // The reason for having this try block (and EnableRaisingEvents toggling)
    // is to solve a known problem with FileSystemWatcher, in which the event
    // is fired twice.

    try
    {
        MessageBox.Show("File changed.");

        watcher.EnableRaisingEvents = false; // Error: The name 'watcher' does
                                             // not exist in the current context
    }
    finally
    {
        watcher.EnableRaisingEvents = true; // Error: The name 'watcher' does
                                            // not exist in the current context
    }
}


这篇关于从另一个方法访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆