0
votes

I have been working on a code that will grab the userName from the local machine and place it in a log file and start a .exe if the username has not been recoreded into the log file. I was able to run the code on my local machine, but when I put it on the server and run it I am given an error that reads:

"Unhandeled exception has occurred in your applications"

Details:

See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

***** Exception Text ******* System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at System.Diagnostics.Process.Start(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start(String fileName) at User.Form1.Form1_Load(Object sender, EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

And here is my code////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace User
{
    public partial class Form1 : Form
    {
        public const string dir = @"C:\Numara";
        public const string path = dir + @"\Audit.log";
        public const string TrackIT = @"\\tkahd-nti-1\TrackIt\Audit32.exe /Q";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //returns user name
            //label1.Text = System.Environment.UserName.ToString();
            string userName = System.Environment.UserName;  //user name

            if (!Directory.Exists(dir))
                //directory does not exist
                //create it
                Directory.CreateDirectory(dir);  //creates directory


            //by this point directory is created

            //now check file
            if (!File.Exists(path))
                //file does not exist, so create it
                File.Create(path);

            //Read data from the .dat file
            string line = System.IO.File.ReadAllText(path);

            //if the name of the logged in user
            //is the same as the user name of the text file
            //then exit program
            if (line == userName)
                Application.Exit();

            else
            //clear fields and write new name to file and begin audit
            {
                //clears fields
                using (FileStream stream = new FileStream(@"C:\Numara\Audit.log", FileMode.Create))
                {
                    using (TextWriter writer = new StreamWriter(stream))
                    {
                        //writer.Write("");
                        writer.Write(userName);
                    }
                    // writes new name to file
                }

                //StreamReader textIn =
                //  new StreamReader(
                //    new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));


                //begins audit

                Process.Start(TrackIT);
                Application.Exit();
            }        
        }
    }
 }
2
Really didn't need to see all of that.John Saunders
@John Saunders - I edited the question to not include all the various linked exceptions. Should clean up the thread.JonH
@JonH: don't need all that irrelevant code, either.John Saunders

2 Answers

3
votes

The exception is being thrown because the process can't find the following network path:

\\tkahd-nti-1\TrackIt\Audit32.exe

The most likely cause is that the user account that the application is running under doesn't have access to that directory.

From your comments below it seems that the app is running using the permissions of the user who is logging on. You would need to grant anyone who might login read-only access to the "Audit32.exe" application.

However you don't need your own application to do this. If you open "Local Security Policy" from "administrative tools" (usually in control panel) you can open up the Local Policies --> User Rights Assignment folder from the tree view on the left and then change the "Log on locally" and "deny logon locally" settings to allow/deny login to individual users or groups of user. but be careful not to lock yourself out of the machine.