Skip to content

fix(file): 缺少默认打开程序时使用记事本兜底#3087

Merged
SALTWOOD merged 1 commit into
devfrom
fix/3085-openpath-no-association
Jun 8, 2026
Merged

fix(file): 缺少默认打开程序时使用记事本兜底#3087
SALTWOOD merged 1 commit into
devfrom
fix/3085-openpath-no-association

Conversation

@SALTWOOD

@SALTWOOD SALTWOOD commented Jun 8, 2026

Copy link
Copy Markdown
Member

This PR resolves #3086.

Summary by Sourcery

Bug Fixes:

  • 确保对于没有关联默认应用程序的文件,仍然可以通过在记事本中打开它们作为回退方案来打开。
Original summary in English

Summary by Sourcery

Bug Fixes:

  • Ensure files without an associated default application are still opened by launching them in Notepad as a fallback.

@pcl-ce-automation pcl-ce-automation Bot added 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 size: S PR 大小评估:小型 labels Jun 8, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 8, 2026

Copy link
Copy Markdown
审阅者指南(在小型 PR 上折叠显示)

审阅者指南

在打开文件时增加错误处理逻辑:当没有关联的默认程序时回退到使用记事本(Notepad)打开,防止在打开某些文件时发生崩溃。

OpenPath 回退到 Notepad 的时序图

sequenceDiagram
    participant Caller
    participant Basics
    participant Process

    Caller->>Basics: OpenPath(path, workingDirectory)
    Basics->>Process: Start(ProcessStartInfo pathPsi)
    alt associated_program_exists
        Process-->>Basics: process_started
    else no_associated_program_and_file_exists
        Basics->>Process: Start(ProcessStartInfo notepadPsi)
    end
Loading

文件级变更

变更 详情 文件
为在打开路径时新增 Win32 特定的错误处理逻辑,当不存在关联的默认应用时回退到 Notepad。
  • 导入 ComponentModel 以访问 Win32Exception 及其 NativeErrorCode。
  • 将针对目标路径的 Process.Start 调用包裹在 try-catch 块中。
  • 当目标文件存在时,捕获 NativeErrorCode 为 1155(指定文件没有关联应用程序)的 Win32Exception。
  • 在该特定错误发生时,创建新的 ProcessStartInfo 使用 notepad.exe 打开文件,将路径作为带引号的参数传入,并禁用 shell execute。
PCL.Core/App/Basics.cs

与关联 Issue 的匹配情况评估

Issue 目标 是否解决 说明
#3086 防止在通过 Basics.OpenPath 打开文件且不存在关联默认程序(错误 1155)时,启动器因未捕获的 Win32Exception 而崩溃。
#3086 为没有关联程序的日志文件提供回退行为:当发生 Win32Exception 1155 且目标文件存在时,用 Notepad 打开它们。

可能相关的 Issue


提示与命令

与 Sourcery 交互

  • 触发新的审阅: 在 pull request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub Issue: 回复 Sourcery 的审阅评论,要求其从该评论创建 issue。你也可以直接回复该审阅评论 @sourcery-ai issue 来从中创建 issue。
  • 生成 pull request 标题: 在 pull request 标题中的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文的任意位置写上 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 一次性解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。当你已经处理完所有评论且不想再看到它们时非常有用。
  • 一次性忽略所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审阅。特别适用于你想从头开始一次新的审阅时——别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的体验

访问你的 控制面板 以:

  • 启用或禁用审阅功能,例如 Sourcery 自动生成的 pull request 摘要、审阅者指南等。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds error-handling around file opening to fall back to Notepad when there is no associated default program, preventing crashes when opening certain files.

Sequence diagram for OpenPath fallback to Notepad

sequenceDiagram
    participant Caller
    participant Basics
    participant Process

    Caller->>Basics: OpenPath(path, workingDirectory)
    Basics->>Process: Start(ProcessStartInfo pathPsi)
    alt associated_program_exists
        Process-->>Basics: process_started
    else no_associated_program_and_file_exists
        Basics->>Process: Start(ProcessStartInfo notepadPsi)
    end
Loading

File-Level Changes

Change Details Files
Add Win32-specific error handling when opening a path and fall back to Notepad if there is no associated default application.
  • Import ComponentModel to access Win32Exception and its NativeErrorCode.
  • Wrap Process.Start for the target path in a try-catch block.
  • Catch Win32Exception with NativeErrorCode 1155 (no application is associated with the specified file) when the target file exists.
  • On that specific error, invoke a new ProcessStartInfo to open the file with notepad.exe, passing the path as a quoted argument and disabling shell execute.
PCL.Core/App/Basics.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#3086 Prevent the launcher from crashing with an uncaught Win32Exception when opening a file via Basics.OpenPath if there is no associated default program (error 1155).
#3086 Provide a fallback behavior for log files without an associated program by opening them in Notepad when Win32Exception 1155 occurs and the target file exists.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - 我发现了 1 个问题,并给出了一些高层面的反馈:

  • 当前的回退路径假定 path 一定是文件;建议在执行 notepad.exe 回退前增加一个 !Directory.Exists(path) 判断,这样目录路径(或不存在的路径)就不会触发不合适的记事本启动。
  • 1155 这个原生错误码是一个魔法数字;建议用一个具名常量或辅助方法来替代(例如 NoApplicationAssociatedErrorCode),以便更清晰地表达意图,并避免在代码库中到处散落原始的 Win32 错误码。
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- 当前的回退路径假定 `path` 一定是文件;建议在执行 `notepad.exe` 回退前增加一个 `!Directory.Exists(path)` 判断,这样目录路径(或不存在的路径)就不会触发不合适的记事本启动。
- `1155` 这个原生错误码是一个魔法数字;建议用一个具名常量或辅助方法来替代(例如 `NoApplicationAssociatedErrorCode`),以便更清晰地表达意图,并避免在代码库中到处散落原始的 Win32 错误码。

## Individual Comments

### Comment 1
<location path="PCL.Core/App/Basics.cs" line_range="165" />
<code_context>
+        {
+            Process.Start(psi);
+        }
+        catch (Win32Exception ex) when (ex.NativeErrorCode == 1155 && File.Exists(path))
+        {
+            Process.Start(new ProcessStartInfo
</code_context>
<issue_to_address>
**suggestion:** 避免直接使用 Win32 错误码 1155 这种魔法数字。

直接使用原始的 `1155` 会让这个条件更难阅读和维护。请使用具名常量(例如 `ERROR_NO_ASSOCIATION = 1155`)或一个共享的枚举/集中定义的 Win32 错误码集合,以清晰表达意图并避免拼写错误。

建议实现方式:

```csharp
        catch (Win32Exception ex) when (ex.NativeErrorCode == ErrorNoAssociation && File.Exists(path))

```

1. 在合适的共享位置为该 Win32 错误码定义一个具名常量。例如,在包含此方法的同一个类中:
   - `private const int ErrorNoAssociation = 1155;`
   如果已经有集中存放 Win32 错误码的位置,则将其添加到那里(例如 `Win32ErrorCodes` 静态类或枚举),并相应更新调用处:
   - `ex.NativeErrorCode == Win32ErrorCodes.ErrorNoAssociation`2. 确保常量名称与条件中使用的名称一致(`ErrorNoAssociation``Win32ErrorCodes.ErrorNoAssociation`)。
</issue_to_address>

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've found 1 issue, and left some high level feedback:

  • The fallback path currently assumes path is a file; consider guarding the notepad.exe fallback with a !Directory.Exists(path) check so directories (or non-existent paths) don’t trigger an inappropriate Notepad launch.
  • The 1155 native error code is a magic number; consider replacing it with a named constant or helper (e.g., NoApplicationAssociatedErrorCode) to make the intent clearer and avoid scattering raw Win32 codes through the codebase.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The fallback path currently assumes `path` is a file; consider guarding the `notepad.exe` fallback with a `!Directory.Exists(path)` check so directories (or non-existent paths) don’t trigger an inappropriate Notepad launch.
- The `1155` native error code is a magic number; consider replacing it with a named constant or helper (e.g., `NoApplicationAssociatedErrorCode`) to make the intent clearer and avoid scattering raw Win32 codes through the codebase.

## Individual Comments

### Comment 1
<location path="PCL.Core/App/Basics.cs" line_range="165" />
<code_context>
+        {
+            Process.Start(psi);
+        }
+        catch (Win32Exception ex) when (ex.NativeErrorCode == 1155 && File.Exists(path))
+        {
+            Process.Start(new ProcessStartInfo
</code_context>
<issue_to_address>
**suggestion:** Avoid using the magic Win32 error code 1155 directly.

Using the raw `1155` makes this condition harder to read and maintain. Please use a named constant (e.g. `ERROR_NO_ASSOCIATION = 1155`) or a shared enum/centralized definition for Win32 error codes to clearly document the intent and avoid typos.

Suggested implementation:

```csharp
        catch (Win32Exception ex) when (ex.NativeErrorCode == ErrorNoAssociation && File.Exists(path))

```

1. Define a named constant for this Win32 error code in an appropriate shared location. For example, within the same class that contains this method:
   - `private const int ErrorNoAssociation = 1155;`
   or if there is an existing central place for Win32 error codes, add it there (e.g., a `Win32ErrorCodes` static class or enum) and update the usage accordingly:
   - `ex.NativeErrorCode == Win32ErrorCodes.ErrorNoAssociation`.
2. Ensure the constant name matches what you use in the condition (`ErrorNoAssociation` or `Win32ErrorCodes.ErrorNoAssociation`).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread PCL.Core/App/Basics.cs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 16c8f222cd

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread PCL.Core/App/Basics.cs

@LuLu-ling LuLu-ling left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AresConnor AresConnor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SALTWOOD SALTWOOD merged commit c27276d into dev Jun 8, 2026
3 checks passed
@pcl-ce-automation pcl-ce-automation Bot added 👌 完成 相关问题已修复或功能已实现,计划在下次版本更新时正式上线 and removed 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 labels Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: S PR 大小评估:小型 👌 完成 相关问题已修复或功能已实现,计划在下次版本更新时正式上线

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C#]: 查看日志时若 .log 文件无默认关联程序会导致未捕获 Win32Exception

3 participants