我有以下變量:多行字符串文本格式給我的錯誤 - 輸入字符串的不正確的格式
var setting =
[email protected]"worker_processes {{0}};
worker_rlimit_nofile {{1}};
error_log logs/{{2}} {{3}};
events
{{
worker_connections {{4}};
multi_accept {{5}};
}}";
當我做了以下String.Format()
操作:
return string.Format(setting,
this.worker_processes,
this.worker_rlimit_nofile,
this.error_log_file,
this.error_log_level,
this.worker_connections
this.multi_accept ? "on" : "off");
我收到以下錯誤:輸入字符串格式不正確。
有什麼建議嗎?
編輯 - 固定
感謝喬恩斯基特,我已經在這個解決方案抵達,而無需使用String.Format()
:
return
[email protected]"worker_processes { this.worker_processes };
worker_rlimit_nofile { this.worker_rlimit_nofile };
error_log logs/{ this.error_log_file } { this.error_log_level };
events
{{
worker_connections { this.worker_connections };
multi_accept { (this.multi_accept ? "on" : "off") };
}}";
如果你使用的是C#5.0(根據你的標籤)'$ @'根本不應該工作 - 並且不清楚你爲什麼試圖使用它。你究竟在努力實現什麼? –
注意:有6個佔位符和4個參數...(這應該給出不同的錯誤 - 所以假設是複製粘貼錯誤)。 –
@JonSkeet對不起,我打算說我正在使用C#4.5。我有一個爲nginx配置文件保存「模板」的變量。然後我想用設置中的值替換佔位符。 – Latheesan