我已经获得了应读取path from app.Settings的代码,但是现在我在那里有价值,当我以.exe文件形式发布该项目时,我尝试在成功安装后将其安装在另一台计算机上,然后运行它,并在。我认为它看起来可能像这样: private static string _ConnectionString; public static string ConnectionString { get { if (_ConnectionString == null) _ConnectionString = FunctionToDynamicallyCreateConnectionstring(); return _ConnectionString; } } private static string FunctionToDynamicallyCreateConnectionstring() { string path = Properties.Settings.Default.swPath; if (path != null) { StreamReader sr = new StreamReader(File.Open(path, FileMode.Open)); SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); cb.DataSource = DecodeFrom64(sr.ReadLine()); cb.InitialCatalog = DecodeFrom64(sr.ReadLine()); cb.UserID = DecodeFrom64(sr.ReadLine()); cb.Password = DecodeFrom64(sr.ReadLine()); } return cb.ToString(); }但是它给出以下错误:system.IO.directorynotfound-是的,我知道为什么,因为它不在If的排列之内。但是我不确定如何改进它,因此,如果在特定计算机上找不到该路径,该程序通常将继续执行,直到我设置了正确的路径为止。谢谢大家的时间和回答。 最佳答案 您将cb.ToString放在定义中的右大括号之外。 private static string FunctionToDynamicallyCreateConnectionstring() { string path = Properties.Settings.Default.swPath; if (path != null) { StreamReader sr = new StreamReader(File.Open(path, FileMode.Open)); SqlConnectionStringBuilder cb = new SqlConnectionStringBuilder(); cb.DataSource = DecodeFrom64(sr.ReadLine()); cb.InitialCatalog = DecodeFrom64(sr.ReadLine()); cb.UserID = DecodeFrom64(sr.ReadLine()); cb.Password = DecodeFrom64(sr.ReadLine()); return cb.ToString(); } else return string.Empty }请注意,该函数需要返回字符串类型。因此,如果您的路径为空,则需要提供替代的返回值。并在调用代码中处理这种可能性。通常,连接字符串是每个数据库应用程序的基本配置,因此,如果配置不正确,则只有两种可能性:向用户发出警告终止您的应用程序并要求支持协助显示一个对话框,您的用户将在其中插入所需的信息。第二种方法需要您的用户有所了解,因此,我认为在这种情况下(缺少配置设置),最好的选择是使用提示信息终止应用程序(文件名和路径以及终止应用程序的原因)关于c# - IO.directorynotfound-动态SqlConnection字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18231844/
10-13 08:16