Visual Studio 的 C#:已发布的应用程序无法在安装后 运行
C# with Visual Studio: Published App Cannot Run After Installation
我用 C# 编写了一个简单的桌面提醒应用程序,我可以毫无问题地发布和安装它。但是,当我尝试 运行 应用程序时,图标会在系统托盘中显示一秒钟然后消失,然后应用程序不会打开或显示在任何地方。我检查了事件查看器,它说 "Application error" 和“.NET Runtime”
有错误
如果我运行它来自Visual Studio,它可以运行完美。只有当我尝试安装应用程序然后 运行 时才会出现此问题。我还没有在其他电脑上测试过。
我已经在 Visual Studio 的“应用程序”选项卡中检查了我的目标框架,它说的是 .NET Framework 4.5.2。我已经确保它在设置先决条件时匹配,但它并没有解决问题。
"Application Error" 详情:
Faulting application name: Reminder.exe, version: 1.0.0.0, time stamp: 0x5d47ba36
Faulting module name: KERNELBASE.dll, version: 10.0.17134.885, time stamp: 0x59816e73
Exception code: 0xe0434352
Fault offset: 0x00112cf2
Faulting process id: 0x1b10
Faulting application start time: 0x01d54b590d580089
Faulting application path: C:\Users\charl\AppData\Local\Apps.0TG7Y9JQ.2CR[=12=]BOH7HGZ.WVN\remi..tion_71aa56c27f5d79b6_0001.0000_31f9cf6345a508a8\Reminder.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: d1a4e88f-6eb2-4655-a7fe-bbff68de9c4c
Faulting package full name:
Faulting package-relative application ID:
“.NET 运行时”详细信息:
Application: Reminder.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at Reminder_desktop_application.FileStreamer.GetData()
at Reminder_desktop_application.TaskControler.LoadTasks()
at Reminder_desktop_application.Reminder..ctor()
at Reminder_desktop_application.Program.Main()
错误似乎来自:
获取数据:
public string[] GetData()
{
try
{
data = System.IO.File.ReadAllLines(FILE_NAME);
return data;
}
catch (FileNotFoundException e)
{
throw new FileNotFoundException(e.ToString());
}
}
和 LoadTasks:
public void LoadTasks()
{
string[] lines = dataStreamer.GetData();
string[] words;
foreach(string line in lines)
{
words = line.Split(',').ToArray<string>();
this.Add(new Task(words[2], Convert.ToDateTime(words[0]), TimeSpan.Parse(words[1]), Convert.ToBoolean(words[3])));
}
}
您可以检查文件是否存在,它将 return 文件内容,如果不存在,它将 return 一个空数组,如下所示:
public IEnumerable<string> GetData()
{
// if file not exist return empty list
return !File.Exists(FILE_NAME) ? Enumerable.Empty<string>() : File.ReadAllLines(FILE_NAME);
}
现在你可以像这样调用你的方法
dataStreamer.GetData().ToArray();
我用 C# 编写了一个简单的桌面提醒应用程序,我可以毫无问题地发布和安装它。但是,当我尝试 运行 应用程序时,图标会在系统托盘中显示一秒钟然后消失,然后应用程序不会打开或显示在任何地方。我检查了事件查看器,它说 "Application error" 和“.NET Runtime”
有错误如果我运行它来自Visual Studio,它可以运行完美。只有当我尝试安装应用程序然后 运行 时才会出现此问题。我还没有在其他电脑上测试过。
我已经在 Visual Studio 的“应用程序”选项卡中检查了我的目标框架,它说的是 .NET Framework 4.5.2。我已经确保它在设置先决条件时匹配,但它并没有解决问题。
"Application Error" 详情:
Faulting application name: Reminder.exe, version: 1.0.0.0, time stamp: 0x5d47ba36
Faulting module name: KERNELBASE.dll, version: 10.0.17134.885, time stamp: 0x59816e73
Exception code: 0xe0434352
Fault offset: 0x00112cf2
Faulting process id: 0x1b10
Faulting application start time: 0x01d54b590d580089
Faulting application path: C:\Users\charl\AppData\Local\Apps.0TG7Y9JQ.2CR[=12=]BOH7HGZ.WVN\remi..tion_71aa56c27f5d79b6_0001.0000_31f9cf6345a508a8\Reminder.exe
Faulting module path: C:\WINDOWS\System32\KERNELBASE.dll
Report Id: d1a4e88f-6eb2-4655-a7fe-bbff68de9c4c
Faulting package full name:
Faulting package-relative application ID:
“.NET 运行时”详细信息:
Application: Reminder.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException
at Reminder_desktop_application.FileStreamer.GetData()
at Reminder_desktop_application.TaskControler.LoadTasks()
at Reminder_desktop_application.Reminder..ctor()
at Reminder_desktop_application.Program.Main()
错误似乎来自: 获取数据:
public string[] GetData()
{
try
{
data = System.IO.File.ReadAllLines(FILE_NAME);
return data;
}
catch (FileNotFoundException e)
{
throw new FileNotFoundException(e.ToString());
}
}
和 LoadTasks:
public void LoadTasks()
{
string[] lines = dataStreamer.GetData();
string[] words;
foreach(string line in lines)
{
words = line.Split(',').ToArray<string>();
this.Add(new Task(words[2], Convert.ToDateTime(words[0]), TimeSpan.Parse(words[1]), Convert.ToBoolean(words[3])));
}
}
您可以检查文件是否存在,它将 return 文件内容,如果不存在,它将 return 一个空数组,如下所示:
public IEnumerable<string> GetData()
{
// if file not exist return empty list
return !File.Exists(FILE_NAME) ? Enumerable.Empty<string>() : File.ReadAllLines(FILE_NAME);
}
现在你可以像这样调用你的方法
dataStreamer.GetData().ToArray();