编写项目启动程序:如何从INI文件向EXE程序传递参数?
It sounds like you want your Loader.exe to access the full contents of your INI file (rather than just individual extracted parameters like User or UID). Your current code uses Resources.Start for the arguments, which likely isn’t pointing to the right value—let’s fix that with practical, reliable solutions.
Option 1: Pass the INI File Path (Recommended)
This is the most robust approach. Instead of sending the file content directly, you pass the full path to your INI file. Loader.exe can then read and parse the file on its own, which avoids command-line length limits and keeps your code clean.
Step-by-Step Implementation:
- First, define the full path to your INI file (use
Path.Combineto avoid path separator issues):string iniFilePath = Path.Combine(BasePath, "config.ini"); // Replace "config.ini" with your actual INI filename - Update your process start code to pass this path as an argument (wrap in quotes to handle spaces in paths):
process.StartInfo.FileName = Path.Combine(BasePath, "Loader.exe"); process.StartInfo.Arguments = $"\"{iniFilePath}\""; process.StartInfo.WorkingDirectory = BasePath; - In
Loader.exe, add code to read the INI file from the provided path. For example, usingFile.ReadAllText()or a dedicated INI parsing library (likeIniParserfor .NET) to extract the[Data]and[Path]sections.
Option 2: Pass the INI File Content Directly
If you absolutely need to send the entire INI content as an argument (instead of just the path), you can read the file into a string and pass it as a quoted value. Note: This only works for small INI files—command-line arguments have strict length limits on most systems.
Implementation:
- Read the INI content first, then escape quotes to preserve formatting:
string iniContent = File.ReadAllText(Path.Combine(BasePath, "config.ini")); // Escape existing quotes and wrap the entire content in quotes string escapedContent = iniContent.Replace("\"", "\\\""); - Update your process start code to pass the escaped content:
process.StartInfo.FileName = Path.Combine(BasePath, "Loader.exe"); process.StartInfo.Arguments = $"\"{escapedContent}\""; process.StartInfo.WorkingDirectory = BasePath; - In
Loader.exe, retrieve the argument and parse it as INI content directly.
Why Your Current Approach Might Be Failing
If Resources.Start isn’t set to either the INI file path or its full content, Loader.exe won’t receive the data it needs. Double-check what Resources.Start contains—if it’s a placeholder or incorrect value, replacing it with one of the above options will resolve the issue.
内容的提问来源于stack exchange,提问作者Nasdomlan

