guys i'm still new on developing system and I've encountered this
This is the string i use to connect. It is inside of a class.
public string connections1 = "user id=sa;" +
"password=;server=SEAN\\SQLEXPRESS;" +
"Trusted_Connection= false;" +
"database= METROEXPRESS; ";
How do i change this to put the connection information in an .ini file?
You'd not using an .ini file; you'd use a .config file, which is an XML file containing your configuration. If you're doing a rich client app (console, winforms, wpf) add an Application Configuration to your project and go from there (look up the section). If you're doing an asp.net application, you should already have a web.config, which you can also add a section to.
Here's an example: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/9a8c9f5a-092e-4c4a-87bb-9f35d8f55da1/
.NET and C# don't have built-in mechanism to edit *.ini files. The standard format for configuration files in .NET is XML.
Check the next question if you still want to use *.ini files.
Reading/writing an INI file
Related
I used Settings.settings interface in VS2022 to add some user settings and a connection string. But I am unable to read the App Settings.
Reading the connection string works using
string sDBConn = System.Configuration.ConfigurationManager.ConnectionStrings["MissingSamplesGUI.Properties.Settings.DBConn"].ConnectionString;
However, I cannot read the User Settings from the same App.Config file. Using
int iFormTop = (int)System.Configuration.ConfigurationManager["FormTop"];
Does not work. How do I read and write these settings?
It's done as follows.
Properties.Settings.Default.FormTop;
This line SqlDatabase sqlDatabase = EnterpriseLibraryContainer.Current.GetInstance<SqlDatabase>(connectionName);
in my application is used to retrieve connection string from config file but the problem there is nearly 4 web config files as
*web.config
*web.debug.config
*web.ct7.config
i dont know from which file the connection is retrieved. any idea how to find it out. please share.
i tried changing the values in all config files but problem is the application does not load if we change and run again. also i changed at runtime but couldnt find it out
We can use this line of code string config = System.AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.This gives the value of which config file is being retrieved & used.So if there is N layers in your application & each layer has a config file & if u want to find which config file is read the above code can be used.
I am trying to edit a config file of another application with my Web Service.
Currently I have:
[WebMethod]
public string EditConfig(string path)
{
string cfgPath = Path.Combine(path, "Compressor.exe.config");
var configMap = new ExeConfigurationFileMap { ExeConfigFilename = cfgPath };
var cf = ConfigurationManager.OpenMappedExeConfiguration(configMap,
ConfigurationUserLevel.None);
string hello = cf.AppSettings.Settings["SaveTo"].Value;
return hello;
//cf.AppSettings.Settings["RandomAddedKey"].Value = "Hello World!";
//cf.Save();
}
I have also tried:
var config = ConfigurationManager.OpenExeConfiguration(path);
var hello = config.AppSettings.Settings["SaveTo"].Value;
return hello;
Sadly I haven't had success with any of these, and before resorting to the XML Editor I needed to make sure that this isn't possible or I am doing something wrong?
I have searched through StackOverFlow and the best results returned me an "Object reference not set to an instance of an object." Exception.
I have tried feeding the exe to the path the config and only the directory in several different applications.
In this case I want the Web Service to tell the Compressor what to compress and where to save it, but in the code I am just experimenting to get it working.
I am using .NET Framework 3.5 (Latest for web services).
I have tried editing apps in different kinds of framework versions and the end result was the same.
Thank you in advance.
If I get it right, what you want to do is manipulate a configuration file of another application programmatically. Your problems have nothing to do with doing your manipulation inside a web service application. While I think it is relatively easy to manipulate a configuration file through the very same application, it is not such an easy task to do it from another application. I would suggest two approaches:
Use the .NET API to do it. Here you can find an example of manipulating configuration files remotely.
You could manipulate the specific configuration file, as every other XML file using the respective .NET libraries. Here you can see how to do it.
Hope I helped!
I have developed a c# desktop application that needs to be hosted on a server and will be scheduled to fetch data based on queries stored in XML files. while developing, I was using the following code to read XML files:
var query = new XPathDocument(#"C:\\Documents and Settings\\XYZ\\Desktop\\productplanningquery.xml");
as you can see I had put the XML file conveniently on my desktop and it worked fine while development. What I want to do now, is to give at a path such that where ever I host the application plus the XML files, it does not throw an exception. One way i thought could be to have a folder in a directory where the application will be installed but for that i will have to figure out the path to current directory dynamically (which i could not figure out).
Please help.
You could pass the location of you XML using args this way you're code would look like so:
var query = new XPathDocument(args[0])
You can also use relative path. Make sure that when deploying your code you keep the location of the file in the same relative location. For example if you place the XML in the same directory as the application
var query = new XPathDocument("productplanningquery.xml")
I am not sure what you want to achieve whether you want to read xml using winform app and do some operation and then pass it web app or some thing else. But here is my understandings:
Case 1: If you need to create XML outside the IIS and that XML will be consumed by ASP.Net app, then :
For using a desktop application with IIS server , you need to have full administrative access to the Live Machine. If its not then you should consider building windows services to operate on the XML files or any task that will run behind the scenes to decrease the load of the asp.net app.
Still in this case if you dont own a server then you need some Virtual Private Hosting or similar kind of hosting where you have almost all previleges to access the system. Then deploy the Windows Service, set the output path in such a manner so that it can be accessed by asp.net app too. And do whatever you want.
Case 2: If you want o read XML in ASP.Net Solely, then
In this case you case read it easily by using XDocument.But note, XML should in the same application directory or under the reach of the ASP.Net app
MSDN Article for Web-Windows Services with ASP.Net
You can use something like this:
var path = string.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "productplanningquery.xml");
var pathForCurrentApp = string.Format("{0}\\{1}", Environment.CurrentDirectory, "productplanningquery.xml");
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?