Link to home
Start Free TrialLog in
Avatar of Karl66
Karl66

asked on

C# VS2008 Windows App: Get error when running method twice "A column named 'Name' already belongs to this DataTable"

C# VS2008 Windows App: Get error when running method twice
"A column named 'Name' already belongs to this DataTable"

Is there an easy way to avoid getting this when I let the user crun the method twice? Here is the code that errors when repeated. I understand the nature of the message looking for best "practice"\work around.

            foreach (FileInfo file in files)
            {
                DataRow dr = dt.NewRow();
                //get details of each file using file object
                dr[Name] = file.FullName;
                dr[Size] = file.Length;
                dr[Ext] = file.Extension;
                dr[Created] = file.LastWriteTime;

                dt.Rows.Add(dr);

Karl66
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Hi Karl66

Try the attached code:
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();
    }
}

Open in new window

Sorry, the previous post was for another question.

I think it should be deleted from this question.
SOLUTION
Avatar of iHadi
iHadi
Flag of Syrian Arab Republic image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial