.NET Windows 应用程序不显示消息框或写入日志文件
本文关键字:日志 文件 消息 Windows 应用程序 显示 NET | 更新日期: 2024-10-26 01:50:23
我有一个没有UI的.NET 4.0 Windows应用程序。所有代码都在 Program 类中。应用程序位于网络共享上。这是它所做工作的基础。
- 检查 C:''程序文件 (x86)''Microsoft Office'' 子文件夹
MSACCESS.EXE以确定安装的 Office 版本。- 使用受信任位置更新注册表,将 MS Access 应用复制到该位置。 创建目标文件夹
- 并将主文件夹的内容复制到目标文件夹。
- 完成所有操作后,它将启动MS Access应用程序。
- .NET 应用将关闭。
如果失败,我会有消息框提示,例如不创建目标文件夹或不更新注册表。除了我自己(我写的!)之外的所有用户都不会收到消息框提示或能够写入日志文件。但是,将创建目标文件夹并将文件复制到目标,并且MS Access应用程序在最后开始。
为什么或为什么所有用户都无法获取 msgbox 提示、更新注册表或写入日志?
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;
}
好的,所以我的问题很愚蠢!我已经硬编码了"程序文件(x86)"路径,而不是使用Viney Pandey建议的环境(谢谢!消息框和写入日志都基于是否找到路径。我的硬编码路径适用于64位Windows操作系统,因此(x86),但所有用户都运行32位Windows 7。因此,它们的路径只是"程序文件"而不是"程序文件(x86)",因此整个日志记录和MessageBox内容被绕过。感谢大家的建议!我只是看不到树木的森林,所以这会让我思考!