更新时间:2021-11-16 22:30:42
查看我的答案问题: C#app已安装但未运行 [ ^ ]
在您的情况下,如果您的代码非常大,请按照Mehdi Gholam的建议进行尝试main方法,通常位于Program.cs文件中。
以下示例适用于Windows窗体应用程序。
See my answer for a similar problem: C# app Installed but does not run[^]
In your case, if your code is very large follow the advice from Mehdi Gholam and put a try-catch in the main method, usually located in the file Program.cs.
The example below applies to a Windows Form application.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
catch (Exception ex)
{
// Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
// returns a folder that is usually allowed to write files to
string filePath = System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"MyApplication_CrashInfo.txt");
System.IO.File.WriteAllText(filePath, ex.ToString());
}
}
}
将MyApplication更改为您的应用程序名称。
使用 Exception.ToString() [ ^ ]你得到的完整的堆栈跟踪,它应该指出代码抛出异常的行。
有关Environment.SpecialFolder枚举 [ ^ ]
当您收到错误消息时,您可以希望缩小到代码失败的位置并解决问题。
[已更新]
确保d你希望在exists中创建文件的irectory,你可以尝试这段代码。
Change MyApplication to your application name.
By using Exception.ToString()[^] you get the full stack trace and it should point out the line where the code throws the exception.
See this link for more info about Environment.SpecialFolder Enumeration[^]
When you get the error message you can then hopefully narrow down to where your code fails and fix the problem.
[Updated]
To make sure the directory you want to create the file in exists, you can try this code.
string dummyPath = @"C:\Temp\Tes\Test\Test.txt";
string dir = System.IO.Path.GetDirectoryName(dummyPath);
if (!System.IO.Directory.Exists(dir))
System.IO.Directory.CreateDirectory(dir);
System.IO.File.Create(dummyPath);
嗯..我真的认为问题是你所在的目录试图写不存在。尝试在你的getLogFilePath方法中添加它:
Well..i really think the problem is the directory where you are trying to write doesn't exists. Try adding this in your getLogFilePath method:
string logFilePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\gatesv";
if (!Directory.Exists(logFilePath))
{
Directory.CreateDirectory(logFilePath);
}