简体   繁体   中英

.NET Windows App Not displaying Message Box or writing to a Log file

I have a .NET 4.0 Windows application with no UI. All the code is in the Program class. The application is located on a network share. This is the basics of what it does.

  1. Check the C:\\Program Files (x86)\\Microsoft Office\\ subfolders for
    MSACCESS.EXE to determine what version of Office is installed.
    1. Updates the registry with the Trusted Location to where an MS Access app will be copied to.
    2. Creates a Destination Folder and copies the contents of Master folder to the destination folder.
    3. It starts the MS Access application when all done.
    4. .NET app closes.

I have message box prompts if things fail, like not creating the destination folder for example or not updating the registry. All users except myself (I wrote it!) do NOT get the Message Box prompts OR are able to write to the log file. However the destination folder gets created and files copies to the destination plus the MS Access application starts at the end.

WHY or WHY are all users not able to get the msgbox prompts, update the registry or write to the log?

        private static void UpdateTrustedLocationForAccessVersion(string startAppFolder)
    {
        var officeVersionsFound = new List<String>();

        const string officeFolder = @"C:\Program Files (x86)\Microsoft Office\";

        //Example of the folder we are looking for
        //C:\Program Files (x86)\Microsoft Office\Office14
        if (Directory.Exists(officeFolder))
        {
            var folders = Directory.GetDirectories(officeFolder, "*.*", SearchOption.TopDirectoryOnly);                
            foreach (var folder in folders)
            {
                if (folder.ToString(CultureInfo.InvariantCulture).ToLower().Substring(officeFolder.Length).StartsWith("office1"))
                {
                    var msAccessFile = Directory.GetFiles(folder, "MSACCESS.EXE", SearchOption.AllDirectories);
                    if (msAccessFile.Length > 0)
                    {
                        officeVersionsFound.Add(folder.Substring(folder.Length-2));
                        MessageBox.Show(folder.Substring(folder.Length - 2) + " was Found!");
                        WriteToLogFile(folder.Substring(folder.Length - 2) + " was Found!");
                    }
                }
            }
        }

    public static void WriteToLogFile(string logMessage)
    {
        string strLogFile = GetLogFile();
        StreamWriter swLog;

        var strLogMessage = string.Format("{0}: {1}", DateTime.Now, "(User " + GetUserName() + " on " + GetMachineName() + ") " + logMessage);

        if (!File.Exists(strLogFile))
        {
            swLog = new StreamWriter(strLogFile);
        }
        else
        {
            swLog = File.AppendText(strLogFile);
        }

        swLog.WriteLine(strLogMessage);
        swLog.Close();
    }

    private static string GetLogFile()
    {
        var logFileName = DateTime.Today.Year.ToString(CultureInfo.InvariantCulture) + DateTime.Today.Month + DateTime.Today.Day + "_Log" + ".txt";
        return _path + logFileName;
    }

    private static string GetUserName()
    {
        return Environment.UserName;
    }

    private static string GetMachineName()
    {
        return Environment.MachineName;
    }

OK, so the problem was pretty dumb on my part! I have hard-coded the "Program Files(x86)" path rather than using the Environment as suggested by Viney Pandey (Thanks!). The MessageBox and Writing to the Log was all based on if the Path was found. My hard coded Path was for a 64bit Windows OS, hence the (x86), BUT all the users run 32 bit Windows 7. Therefore their Path is just "Program Files" and NOT "Program Files(x86)" so the whole logging and MessageBox stuff was bypassed. Thanks to EVERYONE for the suggestions! I just could not see the forest for the trees, so this will make me think!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM