This question already has an answer here:
Is Isolated Storage Always Necessary?
(1 answer)
Closed 7 years ago.
i am writing a simple wp7 application that uses isolated storage concept. i could successfully complete the app but while trying to test if it works properly, i am getting an error stating "abc.xml" file is missing in the zap.
abc.xml is created using the code:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var fs = store.OpenFile("abc.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var root = new XElement("User");
var Mobile_Number = new XElement("Mobile_NUmber", txtMobNum.Text);
var Sec_Ques = new XElement("Sec_Ques", secQues);
var xDoc = new XDocument();
root.Add(Mobile_Number, Sec_Ques);
xDoc.Add(root);
xDoc.Save(fs);
fs.Close();
}
}
if i add abc.xm to the solution, the err is cleared, but i do not think this is the right way to do. can someone help me out
thanks in advance
Any file created in isolated storage on one run of your application isn't considered to be part of the application to be deployed to other devices, no.
If you have a file you want to ship with the application, you should include it explicitly in your project.
Your question is answered here.
Related
I followed the steps described in this tutorial.
I tried to translate the code from Objective-C to Swift. This is the code I have:
var l2tpInterface = SCNetworkInterfaceCreateWithInterface(
kSCNetworkInterfaceIPv4,
kSCNetworkInterfaceTypeL2TP
).takeUnretainedValue();
var pppInterface = SCNetworkInterfaceCreateWithInterface(
l2tpInterface,
kSCNetworkInterfaceTypePPP
).takeUnretainedValue();
var prefs = SCPreferencesCreate(nil,"SoybeanVPN",nil).takeUnretainedValue();
var service = SCNetworkServiceCreate(prefs, pppInterface).takeUnretainedValue();
var success = SCNetworkServiceEstablishDefaultConfiguration(service);
This code returns a runtime error EXEC_BAD_ACCESS(code=1) at the first line.
I googled this error and someone said the problem is caused by using released object in Objective-C? Is that the same in Swift? Does anyone have any experience with VPN creation in Swift?
UPDATE:
I tried to debug and find the const value of kSCNetworkInterfaceIPv4 is invalid, see the attached image:
Is that a bug of Swift?
I believe kSCNetworkInterfaceIPv4 is not the problem, because I can use it just fine to create L2TP/IPSec and Cisco VPNs using Swift.
It's rather tricky to handle C pointers in Swift, but this is how I do it, using Swift 2.3. Notice the wrapping/unwrapping with ! of C-Reference objects.
let initialTopInterface: SCNetworkInterfaceRef!
let initialBottomInterface: SCNetworkInterfaceRef!
// L2TP on top of IPv4
initialBottomInterface = SCNetworkInterfaceCreateWithInterface(kSCNetworkInterfaceIPv4, kSCNetworkInterfaceTypeL2TP)
// PPP on top of L2TP
initialTopInterface = SCNetworkInterfaceCreateWithInterface(initialBottomInterface!, kSCNetworkInterfaceTypePPP)
let service = SCNetworkServiceCreate(usingPreferencesRef, initialTopInterface!)
// Now you assign the attributes
SCNetworkServiceSetName(service, ("Some Name" as CFString))
// myConfiguration is a Hash with your relevant Key/Value pairs
SCNetworkInterfaceSetConfiguration(topInterface!, myConfiguration)
// Here is a good example for why Swift may just crash if you're not careful
let temporaryString:CFString = "IPSec"
SCNetworkInterfaceSetExtendedConfiguration(topInterface!, temporaryString, myL2tpConfiguration)
SCNetworkServiceEstablishDefaultConfiguration(service)
You can find a working implementation (for macOS Sierra) on Github
Just need copy all SCNetworkInterface's
SCNetworkInterfaceCopyAll()
Insert it at the beginning, before creating interfaces
Since Microsoft Web API isn't MVC, you cannot do something like this:
var a = Request.MapPath("~");
nor this
var b = Server.MapPath("~");
because these are under the System.Web namespace, not the System.Web.Http namespace.
So how do you figure out the relative server path in Web API ? I used to do something like this in MVC:
var myFile = Request.MapPath("~/Content/pics/" + filename);
Which would give me the absolute path on disk:
"C:\inetpub\wwwroot\myWebFolder\Content\pics\mypic.jpg"
You can use HostingEnvironment.MapPath in any context where System.Web objects like HttpContext.Current are not available (e.g also from a static method).
var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/SomePath");
See also What is the difference between Server.MapPath and HostingEnvironment.MapPath?
string root = HttpContext.Current.Server.MapPath("~/App_Data");
As an aside to those that stumble along across this, one nice way to run test level on using the HostingEnvironment call, is if accessing say a UNC share: \example\ that is mapped to ~/example/ you could execute this to get around IIS-Express issues:
#if DEBUG
var fs = new FileStream(#"\\example\file",FileMode.Open, FileAccess.Read);
#else
var fs = new FileStream(HostingEnvironment.MapPath("~/example/file"), FileMode.Open, FileAccess.Read);
#endif
I find that helpful in case you have rights to locally test on a file, but need the env mapping once in production.
I can't tell from the context you supply, but if it's something you just need to do at app startup, you can still use Server.MapPath in WebApiHttpApplication; e.g. in Application_Start().
I'm just answering your direct question; the already-mentioned HostingEnvironment.MapPath() is probably the preferred solution.
Since Server.MapPath() does not exist within a Web Api (Soap or REST), you'll need to denote the local- relative to the web server's context- home directory. The easiest way to do so is with:
string AppContext.BaseDirectory { get;}
You can then use this to concatenate a path string to map the relative path to any file.
NOTE: string paths are \ and not / like they are in mvc.
Ex:
System.IO.File.Exists($"{**AppContext.BaseDirectory**}\\\\Content\\\\pics\\\\{filename}");
returns true- positing that this is a sound path in your example
Little bit late answering that but there we go.
I could solve this using Environment.CurrentDirectory
The selected answer did not work in my Web API application. I had to use
System.Web.HttpRuntime.AppDomainAppPath
You can try like:
var path="~/Image/test.png";
System.Web.Hosting.HostingEnvironment.MapPath( # + path)
hi i have a simple question how i can find the path of a file which had been already saved in the isolated storage
using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(App.filePath, FileMode.Create, store))
{
byte[] buffer = new byte[1024];
while (e.Result.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
stream.Close();
}
now i would read this file
i need this path to use it as a parameter of method
Epub epub =new Epub([file path])
any help will be greatly appreciated
If a file is in IsolatedStorage you either put there yourself or it's the one created by the system to store settings.
If you put it there you must have had the path at some point previously. You just need to track the file names (and paths) you're using.
You should not try and access the settings file directly.
Try this
using (var AppStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] filenames=AppStorage.getFileNames();
//choose the filename you want or
//enumerate directories and read file names in each directory
string[] directories=AppStorage.getDirectories();
}
For each directory you have to add the filepath upto that directory just like in any windows file browsing.
Hope it helps.Post your further queries.
There is no need for you to get the path to the file if you are the one who put the file in the isolated storage. The entire guide to how properly read and write files to the app isostore is available here, and this should be your starting point.
The entire reading routine is limited to this:
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read))
{
using (StreamReader reader = new StreamReader(fileStream))
{
Console.WriteLine("Reading contents:");
Console.WriteLine(reader.ReadToEnd());
}
}
Where myIsolatedStorage is equal to IsolatedStorageFile.GetUserStoreForApplication() (akak your local app storage box).
No need for Reflection, as you showed in the comments. The can be relative to a folder, when you're attempting to read, so something like /MyFolder/myFile.txt will work as well, given that the folder exists.
Your problem is this - pushing the relative path in the isostore to the Epub class, which probably does not read directly from the isostore and uses a full path instead. The nature of the Windows Phone OS is such that it won't let a third-party application without proper permissions to access content directly through a full path reference. So you need to figure out a way to pass binary content to the class instead of a path.
I'm new to Firefox addon development, and it's going well so far, but I'm stuck on how to, essentially, download a file from the Web, given a URI, and save it to disk. Mozilla's MDN documentation has information on how to upload files, but the downloading files section is empty and yet to be written. Sadly, I haven't found any documentation which describes how to do this.
Does anyone know of relevant documentation on how to do this?
The old Facebook Photo Album Downloader addon uses this function call in its overlay JavaScript:
saveURL(images[i].replace(/\/s/g, "/n"), null, null, false, true, null);
Obviously, the first argument is the URI to request. The saveURL function isn't defined anywhere, so I assume it's an extension API function. I have tried it in my new addon, and it does work. I would, however, like to know what the other arguments mean.
The standard way to do this is with nsIWebBrowserPersist:
var persist =
Cc["#mozilla.org/embedding/browser/nsWebBrowserPersist;1"].
createInstance(Ci.nsIWebBrowserPersist);
persist.saveURI(serverURI, null, null, null, "", targetFile);
See https://developer.mozilla.org/en/Code_snippets/Downloading_Files for more info.
There actually is some MDN documentation on this: https://developer.mozilla.org/en/Code_snippets/Downloading_Files.
Here's an easy copy/paste option for anyone looking for a quick solution without any further messing about. Put it in your main.js and change the filename, directory and url.
function DownloadFile(sLocalFileName, sRemoteFileName)
{
var saveToDirectory = 'C:\\Users\\louis\\downloads\\';
var chrome = require("chrome");
var oIOService = chrome.Cc["#mozilla.org/network/io-service;1"].getService(chrome.Ci.nsIIOService)
var oLocalFile = chrome.Cc["#mozilla.org/file/local;1"].createInstance(chrome.Ci.nsILocalFile);
oLocalFile.initWithPath(saveToDirectory + sLocalFileName);
var oDownloadObserver = {onDownloadComplete: function(nsIDownloader, nsresult, oFile) {console.log('download complete...')}};
var oDownloader = chrome.Cc["#mozilla.org/network/downloader;1"].createInstance();
oDownloader.QueryInterface(chrome.Ci.nsIDownloader);
oDownloader.init(oDownloadObserver, oLocalFile);
var oHttpChannel = oIOService.newChannel(sRemoteFileName, "", null);
oHttpChannel.QueryInterface(chrome.Ci.nsIHttpChannel);
oHttpChannel.asyncOpen(oDownloader, oLocalFile);
}
DownloadFile("saveAsThis.mp3","http://domain.com/file.mp3");
As of 2015, the APIs for managing (starting, stopping, etc.) downloads have changed since this question was answered. The new APIs are (links to documentation on MDN):
Downloads.jsm
Download
DownloadTarget
PlacesUtils.There is good info at paa's answer to the question: API to modify Firefox downloads list
A Windows Phone 7 app, it seems, has two places with version number - one in AssemblyInfo.cs (via AssemblyVersion/AssemblyFileVersion attributes), the other is WMAppManifest.xml. Those two seem uncorrelated - changing one does not affect the other. The Marketplace, it seems, uses the one from the manifest - can someone please confirm this?
The real question is - how do I retrieve the one from manifest programmatically to display on the About screen?
The WmAppManifest.xml number is in use. First two digits are relevant for Marketplace (it is checked when you do the update) next two are for your internal usage.
This is a regular XML file, open it as a XDocument and parse it. An example.
EDIT: the example is extraneous. For just the version, use:
string Version = XDocument.Load("WMAppManifest.xml")
.Root.Element("App").Attribute("Version").Value;
To get App Version from "WMappManifest.xml", this solution might be a bit more efficient than lukas solution:
For WP7:
var xmlReaderSettings = new XmlReaderSettings
{
XmlResolver = new XmlXapResolver()
};
using (var xmlReader = XmlReader.Create("WMAppManifest.xml", xmlReaderSettings))
{
xmlReader.ReadToDescendant("App");
return xmlReader.GetAttribute("Version");
}
For WP8:
using (var stream = new FileStream("WMAppManifest.xml", FileMode.Open, FileAccess.Read))
{
string appVersion = XElement.Load(stream).Element("App").Attribute("Version").Value;
}