C# 如何在执行或安装时将visual studio中的文件路径修改到数据库 问题
用visualstudio&C编写# 我已经完成了我的程序的编写,它连接到一个SQL数据库,这是目前工作得很好,但因为我想在其他计算机上部署这个程序,它抛出错误,由于不正确的文件路径。例如,在VisualStudio的设置部分,我得到了这个文件,它是我的数据库的文件路径 我试图更改的值的屏幕截图: 当我运行程序或将程序安装到计算机(一旦发布)时,是否有方法更改此值 谢谢你的帮助 谢谢 编辑:我曾考虑使用下面的代码来更改文件路径,但我只想更改此值一次。是否仍要执行此操作?(我尝试了此操作,但由于它是只读的,因此可能无法工作)C# 如何在执行或安装时将visual studio中的文件路径修改到数据库 问题,c#,sql,database,visual-studio,ms-access,C#,Sql,Database,Visual Studio,Ms Access,用visualstudio&C编写# 我已经完成了我的程序的编写,它连接到一个SQL数据库,这是目前工作得很好,但因为我想在其他计算机上部署这个程序,它抛出错误,由于不正确的文件路径。例如,在VisualStudio的设置部分,我得到了这个文件,它是我的数据库的文件路径 我试图更改的值的屏幕截图: 当我运行程序或将程序安装到计算机(一旦发布)时,是否有方法更改此值 谢谢你的帮助 谢谢 编辑:我曾考虑使用下面的代码来更改文件路径,但我只想更改此值一次。是否仍要执行此操作?(我尝试了此操作,但由于
Properties.Settings.Default.Value
Properties.Settings.Default.Save();
编辑2:尝试配置管理器,是否可以使用以下内容更改值:
ConfigurationManager.ConnectionStrings.
设法解决了这个问题,谢谢你的建议
//updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
xElement.FirstChild.Attributes[2].Value = con;
}
}
//writing the connection string in config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
如果有人感兴趣,可以使用OpenFileDialog。数据库现在可以正常加载。您是否想过在安装过程中向用户询问路径?或者如果路径不存在,可以创建路径?我考虑过,但不确定我将如何进行此过程。正在试用新项目,立即安装项目。谢谢您的来信,[已解决],等等。我认为您没有足够的代表。我将在您的回复中添加一个wiki答案。
//updating config file
XmlDocument XmlDoc = new XmlDocument();
//Loading the Config file
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
foreach (XmlElement xElement in XmlDoc.DocumentElement)
{
if (xElement.Name == "connectionStrings")
{
//setting the coonection string
xElement.FirstChild.Attributes[2].Value = con;
}
}
//writing the connection string in config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);