Avatar of John500
John500
Flag for United States of America

asked on 

How to reference a class that's been added to a C Sharp project

Please see the code below which includes two namespaces and two classes:

namespace GetProcessStats
{
    class ProcessInfo
    {
----------------------------

namespace GetProcessStats
{
 
    namespace AutoWriteToFile
    {
        public static class FileWriter
        {
-----------------------------------------------------------
I am having trouble accessing the FileWriter class from Main():

...
using System.IO;
using GetProcessStats.AutoWriteToFile;


class Program
    {
        static void Main(string[] args)
        {

            FileWriter FW = new FileWriter();

            ProcessInfo WscriptInfo = new ProcessInfo();

            WscriptInfo.GetProcess("D011", "mspaint");

            FW.??????   ("", "");
            
        }
    }

Open in new window


What am I missing?
using System;
using System.IO;
using System.Diagnostics;
using GetProcessStats.AutoWriteToFile;

namespace GetProcessStats
{
    class ProcessInfo
    {
        PerformanceCounter pc = new PerformanceCounter();
 
        public Process GetProcess(string hostname, string processName)
        {
            Process[] ps = Process.GetProcessesByName(processName, hostname);
 
            if (ps.Length == 0)
                return null;
            else
                return ps[0];
        }
 
        public bool IsProcessAlive(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return false;
            }
            else
            {
                return !p.HasExited;
            }
        }
 
        public string GetProcessCPU(string hostname, string processName)
        {
            string str = "";
 
            pc.CategoryName = "Process";
            pc.CounterName = "% Processor Time";
            pc.InstanceName = processName;
            pc.MachineName = hostname;
 
            try
            {
                str = pc.NextValue().ToString();
            }
            catch
            {
                str = "Error";
            }
 
            return str;
        }
 
        public string GetProcessRAM(string hostname, string processName)
        {
            Process p = GetProcess(hostname, processName);
 
            if (p == null)
            {
                return "Error";
            }
            else
            {
                return p.PagedMemorySize64.ToString();
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            FileWriter FW = new FileWriter();

            ProcessInfo WscriptInfo = new ProcessInfo();

            WscriptInfo.GetProcess("D011", "mspaint");

            FW. ("", "");
            
        }
    }

}
----------------------------------------------------------------------
Separate code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GetProcessStats
{
 
    namespace AutoWriteToFile
    {
        public static class FileWriter
        {
            // Set this to false to stop writing to file
            public static bool _WriteToFile = true;


            void WriteToFile(string FileDest, string StrOutput)
            {
                TextWriter tw;
                if (_WriteToFile)
                {

                    try
                    {
                        // create a writer and open the file
                        tw = new StreamWriter(FileDest);

                        // write a line of text to the file
                        tw.WriteLine(StrOutput);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);

                    }
                    finally
                    {
                        // close the stream
                        tw.Close();
                    }
                }
            }
        }
    }
}

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
John500

8/22/2022 - Mon