troubleshooting Question

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

Avatar of John500
John500Flag for United States of America asked on
.NET ProgrammingC#
17 Comments2 Solutions357 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 17 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 17 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros