studyzy/imewlconverter · error · ArgumentException
文件路径不能为空
Error message
文件路径不能为空
What it means
FileOperationHelper.GetEncodingType auto-detects a file's text encoding via UtfUnknown. It throws ArgumentException (with paramName=fileName) when the path argument is null or whitespace — a standard input precondition on the public method.
Source
Thrown at src/ImeWlConverter.Core/Helpers/FileOperationHelper.cs:113
sw.WriteLine(line);
return true;
}
catch
{
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");
}View on GitHub (pinned to 16744a12ed)
Solutions
- Validate the path is non-empty at the CLI/GUI trust boundary before calling GetEncodingType.
- If empty/missing files should be tolerated, call ReadFile(path) which returns "" instead of throwing.
- Bind the input field to a required-field validator so submission is blocked when empty.
Example fix
// before
var enc = FileOperationHelper.GetEncodingType(path);
// after
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentException("path required", nameof(path));
var enc = FileOperationHelper.GetEncodingType(path); Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(fileName))
throw new ArgumentException("path required", nameof(fileName)); Try / catch
try { var enc = FileOperationHelper.GetEncodingType(path); }
catch (ArgumentException) { /* empty path, abort or prompt */ } Prevention
- Validate file paths at the trust boundary (CLI argument parsing / GUI submit).
- Bind input-file fields to required-field validators that block submission when empty.
- If missing files are non-fatal, prefer ReadFile(path) which returns "" instead of throwing.
When it happens
Trigger: Calling GetEncodingType(null), GetEncodingType(""), or GetEncodingType(" "); or when a GUI/CLI field bound to a file path is submitted empty.
Common situations: GUI input-file text box left blank but conversion triggered; CLI invoked with an empty -i/--if argument; a glob/wildcard that resolved to an empty path string.
Related errors
AI-assisted analysis of studyzy/imewlconverter@16744a12ed (2026-08-13).
Data as JSON: /api/errors/93c8c909f8fff6a5.
Report an issue: GitHub.