如何通过XAML和C#将GUI输入保存至Perl配置文件?
问题描述
我有一个基于Perl开发的应用,UI由Visual Studio中的XAML和C#实现。已经创建了供源码读取参数的Perl配置文件,现在需要把GUI(XAML)中用户输入的参数通过C#直接保存到这个Perl配置文件里。
现有相关代码如下:
XAML文件
<TabItem Header="Properties" x:Name="tabItem1_Copy" Margin="-2,-2,-2,0"> <Grid Margin="0,0,4.2,2"> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBox Height="20" Margin="295,58,0,0" x:Name="tbShiftColumns" Width="40" /> <TextBlock Height="20" x:Name="textBlock7" Text="Shift Columns" Width="75" /> </Grid> </TabItem>
XAML.cs文件
private void bxxxxxx_Click(object sender, RoutedEventArgs e) { tFLog.Clear(); Process prc = new Process(); prc.StartInfo.FileName = "xxxxxxxxxxxxx.bat"; prc.StartInfo.Arguments = "\"" + tbCustPar.Text + "\" \"" + tbShiftColumns.Text + "\""; prc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;//.Hidden; prc.StartInfo.UseShellExecute = true; prc.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(System.Reflection); prc.Start(); prc.WaitForExit(); prc.Close(); string logFileName = System.IO.Path.GetDirectoryName(System.Reflection) + "\\xxxx.log"; StreamReader sr = new StreamReader(logFileName, System.Text.Encoding.Default, true); tFLog.Text = sr.ReadToEnd(); }
Perl配置文件
#variables to be parsed to xyz.pl file maxDepth = tbNoLevels shiftt = tbshiftColumns
之前试过创建app.config文件,但没法直接保存到Perl配置文件;也尝试过App.xaml,不知道怎么操作。作为新手,需要详细的解决方案或替代方案。
解决方案
核心思路
Perl配置文件是简单的键值对文本格式,直接用C#的文件读写API修改对应键的值即可,不需要依赖app.config这类.NET配置体系。
步骤1:指定配置文件路径
先确定Perl配置文件的绝对路径,推荐用程序目录作为基准路径:
// 假设配置文件和当前程序exe在同一目录下 string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "your_config_file.pl");
步骤2:编写配置修改方法
创建一个通用方法,负责读取配置文件、查找目标键并更新值,最后写回文件:
private void UpdatePerlConfig(string configPath, string key, string newValue) { // 读取配置文件所有行 List<string> lines = File.ReadAllLines(configPath).ToList(); // 遍历每行处理 for (int i = 0; i < lines.Count; i++) { string line = lines[i].Trim(); // 跳过注释行和空行 if (line.StartsWith("#") || string.IsNullOrEmpty(line)) continue; // 分割键和值(按第一个等号分割,避免值里包含等号的情况) string[] parts = line.Split(new[] {'='}, 2); if (parts.Length == 2) { string currentKey = parts[0].Trim(); // 匹配目标键(不区分大小写,按需调整) if (currentKey.Equals(key, StringComparison.OrdinalIgnoreCase)) { // 保留原格式的空格,更新值 lines[i] = $"{currentKey} = {newValue.Trim()}"; break; } } } // 将修改后的内容写回文件 File.WriteAllLines(configPath, lines); }
步骤3:在按钮事件中调用方法
修改现有按钮点击事件,添加保存参数到Perl配置文件的逻辑:
private void bxxxxxx_Click(object sender, RoutedEventArgs e) { tFLog.Clear(); // --- 新增:保存参数到Perl配置文件 --- string configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "your_config_file.pl"); // 更新shiftt参数,对应tbShiftColumns输入框的值 UpdatePerlConfig(configPath, "shiftt", tbShiftColumns.Text); // 如果还有其他参数(比如maxDepth对应tbNoLevels输入框),同样调用方法 // UpdatePerlConfig(configPath, "maxDepth", tbNoLevels.Text); // --- 新增结束 --- Process prc = new Process(); prc.StartInfo.FileName = "xxxxxxxxxxxxx.bat"; prc.StartInfo.Arguments = $"\"{tbCustPar.Text}\" \"{tbShiftColumns.Text}\""; prc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;//.Hidden; prc.StartInfo.UseShellExecute = true; // 修复原代码中WorkingDirectory的错误,正确获取程序目录 prc.StartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); prc.Start(); prc.WaitForExit(); prc.Close(); // 用using语句处理StreamReader,避免资源泄漏 string logFileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "xxxx.log"); using (StreamReader sr = new StreamReader(logFileName, Encoding.Default, true)) { tFLog.Text = sr.ReadToEnd(); } }
关键注意事项
- 替换代码中的
your_config_file.pl为实际的Perl配置文件名 - 确保程序对配置文件所在目录有读写权限(不要放在C:\Windows这类系统目录)
- 如果配置文件的键值对格式有特殊要求(比如固定数量的空格),可以调整
UpdatePerlConfig里的拼接逻辑 - 原代码中
System.Reflection的用法有误,已替换为Assembly.GetExecutingAssembly().Location来正确获取程序路径 - 用
using包裹StreamReader,确保文件流被正确释放
内容的提问来源于stack exchange,提问作者Sai Pradyumna Tumma
相关产品推荐
相关产品推荐

