开发者

Unhandeled exception in application

开发者 https://www.devze.com 2023-02-04 23:45 出处:网络
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

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.Proce开发者_开发技巧ss.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();
            }        
        }
    }
 }


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.


Try fully trusting the share...

http://blogs.msdn.com/b/shawnfa/archive/2004/12/30/344554.aspx?wa=wsignin1.0

0

精彩评论

暂无评论...
验证码 换一张
取 消