{"record":{"id":"e5b2051dd3b6f58f","repo":"studyzy/imewlconverter","slug":"filename","errorCode":null,"errorMessage":"文件不存在: {fileName}","messagePattern":"文件不存在: (.+?)","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"src/ImeWlConverter.Core/Helpers/FileOperationHelper.cs","lineNumber":118,"sourceCode":"            return false;\n        }\n    }\n\n    public static StreamWriter GetWriteFileStream(string path, Encoding coding)\n    {\n        return new StreamWriter(path, false, coding);\n    }\n\n    public static Encoding GetEncodingType(string fileName)\n    {\n        if (string.IsNullOrWhiteSpace(fileName))\n        {\n            throw new ArgumentException(\"文件路径不能为空\", nameof(fileName));\n        }\n\n        if (!File.Exists(fileName))\n        {\n            throw new FileNotFoundException($\"文件不存在: {fileName}\");\n        }\n\n        try\n        {\n            var result = CharsetDetector.DetectFromFile(fileName);\n            var resultDetected = result?.Detected;\n\n            if (resultDetected == null || resultDetected.Confidence < 0.7)\n            {\n                try\n                {\n                    return Encoding.GetEncoding(\"GB18030\");\n                }\n                catch\n                {\n                    return Encoding.GetEncoding(\"GB2312\");\n                }\n            }","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/studyzy/imewlconverter/blob/16744a12ed5064896cf39095163e44b367aaada1/src/ImeWlConverter.Core/Helpers/FileOperationHelper.cs#L100-L136","documentation":"GetEncodingType throws FileNotFoundException when the path is non-empty but no file exists at that location. This is distinct from error [2]: the path is well-formed but points to nothing on disk.","triggerScenarios":"Calling GetEncodingType with a path to a deleted, moved, or typo'd file; a file that existed at selection time but was removed before conversion; a relative path resolved against an unexpected working directory.","commonSituations":"User picked a file then moved/deleted it before converting; relative path resolved from the wrong working directory; a typo in the path; file on an unmounted/removable drive.","solutions":["Check File.Exists(path) immediately before calling GetEncodingType.","Normalize to an absolute path with Path.GetFullPath to avoid working-directory surprises.","Surface a clear 'file not found' message and let the user re-pick the file."],"exampleFix":"// before\nvar enc = FileOperationHelper.GetEncodingType(path);\n// after\nif (!File.Exists(path))\n{\n    ReportError($\"文件不存在: {path}\");\n    return;\n}\nvar enc = FileOperationHelper.GetEncodingType(path);","handlingStrategy":"validation","validationCode":"if (!File.Exists(path)) { ReportError($\"文件不存在: {path}\"); return; }\nvar enc = FileOperationHelper.GetEncodingType(path);","typeGuard":null,"tryCatchPattern":"try { var enc = FileOperationHelper.GetEncodingType(path); }\ncatch (FileNotFoundException ex) { ReportError(ex.Message); }","preventionTips":["Re-check File.Exists immediately before reading; a file chosen earlier may have moved.","Prefer absolute paths (Path.GetFullPath) to avoid working-directory resolution surprises.","Let the user re-pick the file when a missing-file error is reported."],"tags":["file","encoding","io","not-found"],"backgroundTag":null,"analyzedSha":"16744a12ed5064896cf39095163e44b367aaada1","analyzedAt":"2026-08-13T19:53:09.031Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}