C# 字符串。替换不';不能使用php文件吗?
为什么下面的代码不起作用C# 字符串。替换不';不能使用php文件吗?,c#,.net,C#,.net,为什么下面的代码不起作用 string Tmp_actionFilepath = @"Temp\myaction.php"; // change the id and the secret code in the php file File.Copy(@"Temp\settings.php", Tmp_actionFilepath, true); string ActionFileContent = File.Read
string Tmp_actionFilepath = @"Temp\myaction.php";
// change the id and the secret code in the php file
File.Copy(@"Temp\settings.php", Tmp_actionFilepath, true);
string ActionFileContent = File.ReadAllText(Tmp_actionFilepath);
string unique_user_id = textBox5.Text.Trim();
string secret_code = textBox1.Text.Trim();
ActionFileContent.Replace("UNIQUE_USER_ID", unique_user_id);
ActionFileContent.Replace("SECRET_CODE", secret_code);
File.WriteAllText(Tmp_actionFilepath, ActionFileContent);
下面是setting.php的内容
<?php
session_start();
$_SESSION["postedData"] = $_POST;
/////////////////////////////////////////////////////////
$_SESSION["uid"] = "UNIQUE_USER_ID";
$_SESSION["secret"] = "SECRET_CODE";
/////////////////////////////////////////////////////////
function findThis($get){
$d = '';
for($i = 0; $i < 30; $i++){
if(file_exists($d.$get)){
return $d;
}else{
$d.="../";
}
}
}
$rootDir = findThis("root.cmf");
require_once($rootDir."validate_insert.php");
返回一个新字符串,因为字符串是不可变的。它不会替换正在调用它的字符串
您应该替换:
ActionFileContent.Replace("UNIQUE_USER_ID", unique_user_id);
ActionFileContent.Replace("SECRET_CODE", secret_code);
与:
除此之外,您应该真正更改变量名,使其遵循常规的C#命名约定(即使用actionFileContent
而不是actionFileContent
)。返回一个新字符串,因为字符串是不可变的。它不会替换正在调用它的字符串
您应该替换:
ActionFileContent.Replace("UNIQUE_USER_ID", unique_user_id);
ActionFileContent.Replace("SECRET_CODE", secret_code);
与:
除此之外,您应该真正更改变量名,使其遵循常规的C#命名约定(即使用actionFileContent
而不是actionFileContent
)。您必须在字符串上设置replace
string方法的结果
string Tmp_actionFilepath = @"Temp\myaction.php";
// change the id and the secret code in the php file
File.Copy(@"Temp\settings.php", Tmp_actionFilepath, true);
string actionFileContent = File.ReadAllText(Tmp_actionFilepath);
string unique_user_id = textBox5.Text.Trim();
string secret_code = textBox1.Text.Trim();
// set the result of the Replace method on the string.
actionFileContent = ActionFileContent.Replace("UNIQUE_USER_ID", unique_user_id)
.Replace("SECRET_CODE", secret_code);
File.WriteAllText(Tmp_actionFilepath, actionFileContent);
必须对字符串设置replace
string方法的结果
string Tmp_actionFilepath = @"Temp\myaction.php";
// change the id and the secret code in the php file
File.Copy(@"Temp\settings.php", Tmp_actionFilepath, true);
string actionFileContent = File.ReadAllText(Tmp_actionFilepath);
string unique_user_id = textBox5.Text.Trim();
string secret_code = textBox1.Text.Trim();
// set the result of the Replace method on the string.
actionFileContent = ActionFileContent.Replace("UNIQUE_USER_ID", unique_user_id)
.Replace("SECRET_CODE", secret_code);
File.WriteAllText(Tmp_actionFilepath, actionFileContent);