有的时候我们需要在运行时让用户选择连接字符串,比如动态连接数据库时。我们可以这么做: string path = AppDomain.CurrentDomain.BaseDirectory + System.DateTime.Now.ToString().Replace("/","").Replace(@"","").Replace(":","").Replace(" ","")+".udl"; //创建一个临时文件 FileStream fs = null; fs = File.Create(path); fs.Close(); //打开一个进程,直到我们不需要它的时候. using(Process process = new Process()) { process.StartInfo.FileName = path; process.StartInfo.UseShellExecute = true; process.Start(); while(!process.HasExited); } //把连接字符串放到字符中. StreamReader sr = new StreamReader(path); string contents = sr.ReadToEnd(); sr.Close(); File.Delete(path); MessageBox.Show(contents);见效果: