Read from App_Data using Json (.NET) - vb.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.

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")

F# - using relative path for a data catalog in a connection string

Hello I have attached a local DB to my visual studio project but I can't make my absolute path relative.
I have a project with a .fs file located at
C:\USERS\SOME_USER\SOLUTION_FOLDER\DIRECTORY\FIRST_PROJECT\file.fs
In this file I am trying to use a local databse with a catalog in another project and so the generated connection is
#"Data Source=(LocalDB)\v11.0; Initial Catalog=C:\USERS\SOME_USER\SOLUTION_FOLDER\DIRECTORY\OTHER_PROJECT\APP_DATA\LOCALDATABASE.MDF; Integrated Security=True; Connect Timeout=15; Encrypt=False; TrustServerCertificate=False"
This works wonders
Then I tried to make the path to the catalog relative like this "..\OTHER_PROJECT\APP_DATA\LOCALDATABASE.MDF" but that wont work.
The compiled file.fs should be in C:\USERS\SOME_USER\SOLUTION_FOLDER\DIRECTORY\FIRST_PROJECT\BIN\DEBUG\
Which got me thinking that the compiled files need to go 3 directories out and so I have also tried with this catalog
"..\..\..\OTHER_PROJECT\APP_DATA\LOCALDATABASE.MDF" which wont work either.
So I am a bit unsure what to do.
Oh, and the error I am getting when trying a relative path:
ERROR: Error reading schema. ERROR: Cannot open database.
-
MJ
Taken from the docs:
Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName …
The string |DataDirectory| expands to the App_Data folder of the project (this is done by a setting within the SQL Server support in ADO.NET so is language independent).
You should be able to use .. after the |DataDirectory|, otherwise you need to find how to however how that expands (I recall working this out, but it was a long time ago).

How to store and retrieve audio files from a database into VB 2008?

I've been trying to find a way to store a .wav file in a database (Access) and then retrieve it into Visual Basic 2008 application.
I can do it for an image file, but whatever I've tried for audio files has been a failure so far. I have done a lot of searching in different sites, but it seems that no one has had this need since I haven't found a single site that addresses this.
By the way, I don't want to store a pointer to the file on disk by storing the file directory information. I want the actual contents of the file to be stored in the database, the same way that I am doing with the graphics file. Is it possible to do it? How?
Thanks,
Luis
I'd store the .wav in a local file;
Dim filestring as string = application.startuppath & "\filename.wav"
Use the database to hold the filename, then MCI strings etc. to play it from wherever you stored it before.
However if you're quite sure you want to find it from the directory, I'd send the problem in to daniweb, see if they know.

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.

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

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.