Vb.net compact framework and Saving XML (app.config) - vb.net

I am writing my first windows ce app. I am using an xml file (app.Config) to store state data... basically user preferences on the last location (connection string) that the person was using before closing the app. I am having trouble understanding what is happening on deploy. It looks like it is copying my xml file to the debug folder on deploy. My problem is, when I save the xml file it is not saving in my project. Is there another folder in which the emulator resides that contains all of the state data and possibly the file that I am writing out? I am saving the doc to the same filepath as I am reading in but it does not actually save or throw error or anything.
Any help is appreciated!
Thanks

I can think of three possible issues that might cause this behaviour:
The app.config gets renamed at compile/deploy time to MyFirstApp.exe.config. Make sure you are writing back to that filename and not app.config.
app.config normally lives in your source folder and at compile time is copied to the bin\Debug directory. For .NETCF project the MyFirstApp.exe.config is then deployed to the emulator or the device.
Make sure you are writing to the correct directory, this code snippet might help:
string appDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
You are doing everything correct but app.config replaces your changes in MyFirstApp.exe.config everytime your rebuild-all or deploy.

You have to make sure you're reading and writing to the correct place. I was able to get something similar working using this post.

Related

Can't copy to network path using Filesystem.filecopy or FileSystem.CopyFile

I'm trying to copy a file from a local computer to a remote share but I get an exception saying "Can't find the specified file".
My.Computer.FileSystem.CopyFile("C:\filename.jpg", "\\focserver2\consultoria\teste\filename.jpg")
The remote shared folder has full control permissions on "Everyone".
What am I doing wrong? Or it's not possible to copy to network paths using FileSystem.CopyFile ?
Thanks.
João
The issue does not appear to be the destination but the source. I assume that the sample you showed above is NOT the real code and in the real code, you combine a couple values together to define the source. To help prevent issues, get in the habit of using the Path.Combine() method when concatenating strings for a file path. It's a life-saver.
The next most important thing is to learn how to debug your code by setting break-points and seeing what the values of your combined strings are before posting to websites. This is a good one to get you started. http://weblogs.asp.net/scottgu/debugging-tips-with-visual-studio-2010
Most likely the problem is that you are trying to access the root folder of the C:\ drive. This folder is generally locked down by the operating system, and even simple file operations don't work easily there.
Try copying the file from a subfolder e.g.
My.Computer.FileSystem.CopyFile("C:\Temp\filename.jpg", "\\focserver2\consultoria\teste\filename.jpg")

Visual Studio 2010 Setup does not create added file

My problem should be plain and simple to solve, but google is not helping me today.
I need to read/write a configuration file (config.xml) and, as i see so much problems with permissions with special folders, i decided for myDocuments.
Now, from File system (Setup), I added a custom special folder (myDocuments)
added a subfolder (g1OKweb) inside myDocuments
added the file (config.xml) inside g1OKweb
What I expect, reading around, is that during the installation g1OKweb should be created if not existing or older, and the same for config.xml, but it isn't.
Does someone have any clue?
Thanks in advance
Use Directory.CreateDirectory to create the directory before attempting to access the file. This will automatically create all parts of the path that do not yet exist. If the full path already exists, it will do nothing.
When opening the file, use a FileStream constructor overload that allows you to specify FileMode.OpenOrCreate. This will succeed regardless of whether the file already exists or not.
When you have opened the file, check to see if it is empty before parsing it. If it is empty, insert your XML root element first.

Read from App_Data using Json (.NET)

We got a homework in college, to make a Local web-site using MVC and VB, that will read from a Json file (that is places in the App_Data folder) some seed data and populate the database.
The problem is, I'm not sure how to make Json read from the file in App_data, without assigning a full address to it.
What happens, pretty much, is I give it an address:
File.ReadFile("~/App_Data/emails.json") 'Also tried "App_Data/emails.json"
After that, I pass this path (as filename) to a StreamReader:
Using fileRead As New IO.StreamReader(filename)
Json read stuff
And instead of reading this address as the address of the Project ("C:\Users\BlueLight\Desktop\Codes\VBA\NMCAss2\App_Data\emails.json") it throws an Exception, that it
"could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\App_Data\emails.json'." So, it searches in a different place.
Can I redirect this path to my App_Data, without using the full path? Or, maybe there is a problem somewhere in my logic?
Thanks in advance.
Update: I tried reversing "/" with "\" like "App_Data\filename.json" and "'~\App_Data\filename.json" it still didn't work.
Apparently, the best way to fix it is using
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/namemails.json")
Works perfectly.

VB.NET: Changes to application.dll.config file does not reflect inside system

I'm quite new to this app.config function. Hope someone can show me the road on this.
I have a solution which comprise of 5 projects and one of them just to retrieve the value from the app.config file to be read by other projects. So, in the app.config file, i have my connection string stated inside and i can use it nicely.
After i built the projects, it will convert my app.config file to appname.dll.config file and it can be seen in the bin/Release folder. I copy all these projects bin/release content to another folder to have all those files stay in one place.
My question is, which config file should i change if i want to change the connection string username (for example) WIHOUT building the project and transfer again? I changed the value inside appname.dll.config file but it does not reflect when the program run. It's still taking the old connection string.
Hope someone can help me on this.
Thanks in advance.
Usually the app.config associated with the exe project (command line or GUI) is the one that holds the changes app wide. Check for the appname.exe.config file and make your necessary changes there for connection strings, etc...
[update]
Just thought of something else... if it's a web project you can use the web.config in the main webproject to configure your changes

How to Get a File Path in a Specific Project in a Web Application Solution?

I'm writing a Visual Studio Macro and need to read a XML file in my project and write something. i can't get the correct file path!
in my web application solution i have to projects for business and UI files. and my xml file named fa.xml located on UI project in a separate folder. i want to use this macro for many solutions. but the structure is the same.
How can I get the Path of a file in a specific project in my solution?
OK, I'm just going to check for the file. You should get the general idea:
If my.computer.filesystem.fileexists(application.startuppath + "\blabla\fa.xml") then
...
end if