You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#方法间传递用户输入变量以修改MongoDB数据?

解决C#函数间变量访问的问题

别担心,这个问题其实是典型的变量作用域问题——你在downloadAndInstall37CineAgent里定义的HostName和streamName都是局部变量,只能在那个函数内部访问,changeMongo自然找不到它们。下面给你两种靠谱的解决办法:

方案一:通过参数传递变量(推荐)

这是最符合代码规范的做法,把需要用到的变量作为参数传给changeMongo函数,明确函数的依赖关系:

public static void downloadAndInstall37CineAgent() {
    Console.Write("Enter Path to Cert: ");
    string certPath = Console.ReadLine();
    Console.Write("Enter Password to Cert: ");
    string certPass = Console.ReadLine();
    Console.Write("Network Manager Host: ");
    var HostName = Console.ReadLine();
    Console.Write("Streaming Engine Host Name: ");
    var streamName = Console.ReadLine();
    
    // 调用时把获取到的变量传递给changeMongo
    changeMongo(HostName, streamName);
}

// 修改changeMongo函数,添加两个参数接收变量
public static void changeMongo(string hostname, string streamName) {
    var client = new MongoClient("mongodb://localhost:27017");
    IMongoDatabase db = client.GetDatabase("CineNetGlobalSettings");
    var collection = db.GetCollection<BsonDocument>("CineNetSettings");
    
    // 更新NetworkManagerHost
    var filter = Builders<BsonDocument>.Filter.Eq("_id", "NetworkManagerHost");
    var update = Builders<BsonDocument>.Update.Set("Value", hostname);
    collection.UpdateOne(filter, update); // 别忘了执行更新操作!
    
    // 更新NetworkManagerUrl
    var filterUrl = Builders<BsonDocument>.Filter.Eq("_id", "NetworkManagerUrl");
    var updateUrl = Builders<BsonDocument>.Update.Set("Value", hostname);
    collection.UpdateOne(filterUrl, updateUrl);
    
    // 更新WowzaIpAddress
    var filterStream = Builders<BsonDocument>.Filter.Eq("_id", "WowzaIpAddress");
    var updateStream = Builders<BsonDocument>.Update.Set("Value", streamName);
    collection.UpdateOne(filterStream, updateStream);
}

方案二:使用类级静态字段

如果你的程序是单次安装执行的场景,也可以把变量定义为类的静态字段,这样同一个类里的所有静态函数都能访问:

// 在类的顶部定义静态字段(注意访问权限设为private,避免外部误修改)
private static string _hostname;
private static string _streamName;

public static void downloadAndInstall37CineAgent() {
    Console.Write("Enter Path to Cert: ");
    string certPath = Console.ReadLine();
    Console.Write("Enter Password to Cert: ");
    string certPass = Console.ReadLine();
    Console.Write("Network Manager Host: ");
    _hostname = Console.ReadLine(); // 赋值给静态字段
    Console.Write("Streaming Engine Host Name: ");
    _streamName = Console.ReadLine();
    
    changeMongo();
}

public static void changeMongo() {
    var client = new MongoClient("mongodb://localhost:27017");
    IMongoDatabase db = client.GetDatabase("CineNetGlobalSettings");
    var collection = db.GetCollection<BsonDocument>("CineNetSettings");
    
    var filter = Builders<BsonDocument>.Filter.Eq("_id", "NetworkManagerHost");
    var update = Builders<BsonDocument>.Update.Set("Value", _hostname); // 直接使用静态字段
    collection.UpdateOne(filter, update);
    
    var filterUrl = Builders<BsonDocument>.Filter.Eq("_id", "NetworkManagerUrl");
    var updateUrl = Builders<BsonDocument>.Update.Set("Value", _hostname);
    collection.UpdateOne(filterUrl, updateUrl);
    
    var filterStream = Builders<BsonDocument>.Filter.Eq("_id", "WowzaIpAddress");
    var updateStream = Builders<BsonDocument>.Update.Set("Value", _streamName);
    collection.UpdateOne(filterStream, updateStream);
}

小提醒

注意我在代码里补上了collection.UpdateOne的调用——你原来的代码只构建了更新条件,但没有真正执行修改操作,这会导致MongoDB里的数据不会被更新哦!

内容的提问来源于stack exchange,提问作者carlhiggins

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:02:53