studyzy/imewlconverter · error · FileNotFoundException
文件不存在: {fileName}
Error message
文件不存在: {fileName} What it means
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.
Source
Thrown at src/ImeWlConverter.Core/Helpers/FileOperationHelper.cs:118
return false;
}
}
public static StreamWriter GetWriteFileStream(string path, Encoding coding)
{
return new StreamWriter(path, false, coding);
}
public static Encoding GetEncodingType(string fileName)
{
if (string.IsNullOrWhiteSpace(fileName))
{
throw new ArgumentException("文件路径不能为空", nameof(fileName));
}
if (!File.Exists(fileName))
{
throw new FileNotFoundException($"文件不存在: {fileName}");
}
try
{
var result = CharsetDetector.DetectFromFile(fileName);
var resultDetected = result?.Detected;
if (resultDetected == null || resultDetected.Confidence < 0.7)
{
try
{
return Encoding.GetEncoding("GB18030");
}
catch
{
return Encoding.GetEncoding("GB2312");
}
}View on GitHub (pinned to 16744a12ed)
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.
Example fix
// before
var enc = FileOperationHelper.GetEncodingType(path);
// after
if (!File.Exists(path))
{
ReportError($"文件不存在: {path}");
return;
}
var enc = FileOperationHelper.GetEncodingType(path); Defensive patterns
Strategy: validation
Validate before calling
if (!File.Exists(path)) { ReportError($"文件不存在: {path}"); return; }
var enc = FileOperationHelper.GetEncodingType(path); Try / catch
try { var enc = FileOperationHelper.GetEncodingType(path); }
catch (FileNotFoundException ex) { ReportError(ex.Message); } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of studyzy/imewlconverter@16744a12ed (2026-08-13).
Data as JSON: /api/errors/e5b2051dd3b6f58f.
Report an issue: GitHub.