如何从另一个访问一个类

时间:2015-05-26 15:44:04

标签: c# oop

我知道如何创建类的实例等。但由于某种原因,我无法创建我想要的类的实例,因为它没有检测到类甚至在那里。

我正在使用Visual Studio并在C#中编写代码。请帮我。我想访问Program类中的watcher类。

using System;
using System.Net;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {

  public Program()
    {
    }

    // DOWNLOAD
    private void Download(string filePath, string fileName)
    {
        // Get the object used to communicate with the server.
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://50.62.234.1");
        request.Method = WebRequestMethods.Ftp.DownloadFile;

        // This example assumes the FTP site uses anonymous logon.
        request.Credentials = new NetworkCredential("test", "test");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();
    }

    private void Upload (string filePath, string fileName)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://something.com");
        request.Method = WebRequestMethods.Ftp.UploadFile;
    }

    private void ListDirectoryItems()
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://something.com");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

        request.Credentials = new NetworkCredential("test", "test");

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();

        Stream responseStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(responseStream);
        Console.WriteLine(reader.ReadToEnd());

        Console.WriteLine("Directory List Complete, status {0}", response.StatusDescription);

        reader.Close();
        response.Close();
    }

    static void Main(string[] args)
    {
        // List contents of FTP Directory
        Program program = new Program();
        Console.WriteLine("Downloading..... Please wait....");
        // List contents of FTP Directory
        program.ListDirectoryItems();
        // if there is items in the directory download those items
        program.Download("ftp://www.contoso.com/test.htm", "test.htm");
        Console.WriteLine("File Downloaded");

    }
}
}

using System;
using System.IO;
using System.Security.Permissions;

namespace ConsoleApplication2
{
    public class Watcher
    {
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // if the directory is not specified, exit the program
        if (args.Length != 2)
        {

            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create the file watcher and set its properties
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];

        // Watch for changes
        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        // only watch text files for now.
        watcher.Filter = ".txt";

        // Event handlers
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new FileSystemEventHandler(OnChanged);

        // Begin watching
        watcher.EnableRaisingEvents = true;

        // Wait for users to quit the program
        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    // Define the event handlers
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
}
}

1 个答案:

答案 0 :(得分:0)

请为您的观察者课程尝试此操作..

它编译并以智慧显示

using System;
using System.IO;
using System.Security.Permissions;

namespace ConsoleApplication2
{
public class Watcher
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // if the directory is not specified, exit the program
        if (args.Length != 2)
        {

            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create the file watcher and set its properties
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];

        // Watch for changes
        watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.FileName | NotifyFilters.DirectoryName;

        // only watch text files for now.
        watcher.Filter = ".txt";

        // Event handlers
        watcher.Changed += Watcher_Changed;
        watcher.Created += Watcher_Changed;
        watcher.Deleted += Watcher_Changed;
        watcher.Renamed += Watcher_Renamed;

        // Begin watching
        watcher.EnableRaisingEvents = true;

        // Wait for users to quit the program
        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    private static void Watcher_Renamed(object sender, RenamedEventArgs e)
    {
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

    private static void Watcher_Changed(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }
}
}