My application have a small piece of code which read/write a file in C++/CX (Windows Phone 8 Interop DirectX) like this:
bool WriteState(char *fileName) {
FILE *fp = fopen(fileName);
if (fp) {
// Do save
...
fclose(fp);
return true;
}
return false;
}
and ReadState has the same code. and invoking (call from C#):
ObjectClass game = new ObjectClass();
game.WriteState("game1.state");
It's no problem when I run on my device, debug with no error. But after I published to Windows Phone Store and install to my device, the Read/Write code doesn't work. Why this happen?
Make sure you aren't saving the file in its install location. Your app won't have access to its install location in production. You'll need to save in isolated storage.
http://msdn.microsoft.com/en-us/library/windows/apps/jj681698(v=vs.105).aspx
There is this way, that should work for you..
Related
I know c#; have developed for windows mobile; I now have an android project with constraints that I cannot change; the most important being cannot run above API 16 due to the devices the app will be running on. These devices are already purchased. I’m aware of the age of these devices, and how old 4.1 is; hands tied.
I’ve started a new Xamarin (not forms) project for android only; compile using 8.1 Oreo; Min version 4.1, target version 8.1. I’m aware this is not ideal however doing so I’ve managed to get lots of other needed features working including camera and barcode scanner. Changing compile version down to 4.1 causes numerous errors which won’t compile.
I’m testing the device using usb-debugging on the actual device and even though it’s 4.1, the code runs and features work – camera, scanner etc.
I’m stuck trying to create a folder and then write/read a file in that folder. I’d like this folder to be accessible via windows explorer when plugged into computer.
I’ve got code like this to write a file:
string FileContents = "Text file contents";
Java.IO.File SaveFolder = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory("Documents") + Java.IO.File.Separator + "FolderName");
Boolean Success = false;
if (!SaveFolder.Exists())
{
Success = SaveFolder.Mkdirs();
}
string FName = "test.txt";
string FTogether = System.IO.Path.Combine(SaveFolder.Path, FName);
Java.IO.FileWriter fw = new Java.IO.FileWriter(FTogether);
fw.Write(FileContents);
fw.Close();
SaveFolder.Dispose();
And code like this to read the file:
Java.IO.File SaveFolder = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory("Documents") + Java.IO.File.Separator + "FolderName");
string FName = "test.txt";
string FTogether = System.IO.Path.Combine(SaveFolder.Path, FName);
StreamReader sr = new StreamReader(FTogether);
string FileContents = sr.ReadToEnd();
Debugging reveals SaveFolder’s AbsolutePath to be /storage/sdcard0/Documents/FolderName
I think this is part of the android’s internal storage which the app has access to but nothing else.
How do I get a folder which is accessible from outside the app ?
Environment.DirectoryDocuments
cannot be used because it’s null at runtime on API 16.
The path you have is actually publicly accessible so it's not specific to your application. When you connect your Android device to the computer, you should be able to go to the root folder of the device and see the Documents folder.
Don't let the /storage/sdcard0/ part in the beginning of the path confuse you. For historical reasons, Android simulates an sdcard even if there isn't one physically on the device. In reality, /storage/sdcard0/ is just a symlink to /data/media/0.
For a really good overview of Android storage, I'd recommend you read this very thorough Reddit post: Let's clear up the confusion regarding storage in Android once and for all, including adoptable storage in Marshmallow.
What you can also do is download a file explorer app (in case your phone doesn't already have one) and go to the Documents folder. You should see FolderName there.
Edit: Since you're running on such an old version of you might also suffer from the bug in the MTP protocol, which causes newly created files and folders to be invisibile when attaching the device to a computer via USB.
The fix is to call MediaScannerConnection.scanFile for each new file/folder you've created, as explained here.
I know that there are a lot of questions concerning getting a
"System.UnauthorizedAccessException".
However I couldn't find a solution in any of these questions, as most of the answers refer to one of these Microsoft help pages.
My Situation:
I try to save some user input as .csv, so I can import it when needed.
My Code:
var csv = new System.Text.StringBuilder();
string dir = Path.Combine(Environment.GetFolderPath
(Environment.SpecialFolder.DesktopDirectory), "test.csv");
var newLine = string.Format("{0},{1},{2},{3},{4}", txtFirstName.Text, txtLastName.Text, txtEmail.Text,
txtPhone.Text, txtPlace.Text);
csv.AppendLine(newLine);
if (!File.Exists(dir))
{
using (FileStream fs = File.Create(dir))
{
Byte[] info = new System.Text.UTF8Encoding(true).GetBytes("FirstName,LastName,Email,Phone,Place");
// Add headers to the file.
fs.Write(info, 0, info.Length);
}
}
try
{
File.AppendAllText(dir, csv.ToString());
}
catch (Exception ex)
{
throw ex;
}
As you can see, I'm trying to write everything to my Desktop, in a file called "test.csv". I am running Visual Studio as an Administrator and the file I have on my Desktop is not read-only.
Does anybodoy have an idea why this still fails?
Edit: I'm running this as a Standard UWP-App on a desktop Computer.
From a UWP process file access is restricted. In order to write to the desktop (or any arbitrary location) your app will need to use the file save dialog and let the user confirm/choose the location. Then you will be able to save to the desktop or whatever location the user has decided to select.
In the upcoming Spring 2018 update for Windows 10 we will introduce a new capability ('broadFileSystemAccess') for UWP applications that will make this better. If you declare this capability in your manifest, the app will ask for user consent on first launch for broad file system access, and then you will be able to access all locations that the current user has access to.
If you need a solution that works on earlier versions of Windows 10 (prior to Spring 2018 update) and the file dialog is not a viable option then you can look into adding a fulltrust process to your UWP package that handles the file operations on behalf of your UWP process. You can launch that fulltrust process from the UWP via the FullTrustProcessLauncher API: https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.fulltrustprocesslauncher
I'm developing a game for Windows Phone 7.1 with XNA framework.
I know there is a way to prevent piracy for Windows Phone 7 (and 8.0, 8.1) - checking if the file "WMAppPRHeader.xml" exists in app directory.
I'm using this code:
try
{
Stream stream = TitleContainer.OpenStream("WPAppPRHeader.xml");
if (stream.CanRead)
{
stream.ReadByte();
stream.Close();
}
}
catch
{
//file read error, it means it was hacked
}
When use this code and upload my game to Windows phone marketplace as a "Beta" app, it works great. File "WPAppPRHeader.xml" is readable from my game and the test is passed.
But, when upload the same XAP in Windows Phone marketplace as a public app, this code fails and my game thinks that it was hacked (I'm checking this somewhere in the middle, so microsoft testers doesn't event recognise that something is wrong and my game succesfuly passes certification).
So, what am I doing wrong? Why the same code, the same XAP is working when it's Beta, and not working when it's Public?
I still don't know why TitleContainer.OpenStream is working in beta and not working in public market, but here is the code, that works in public market:
System.Xml.Linq.XDocument.Load("WPAppPRHeader.xml");
I've got a Windows Phone 7 Silverlight app that I am trying to use Isolated Storage on the emulator, to no avail. I've got several files added to the project as Content, I've deployed the app to the emulator before launching the debugger, I use the back button to "close" the debugging session, and I do not close the emulator between runs. And every single time, I have empty isolated storage. What am I doing wrong? I use this, comes up blank every time.
private void CheckIsolatedStorageForContent()
{
using ( var isf = IsolatedStorageFile.GetUserStoreForApplication() )
{
foreach ( var fileName in isf.GetFileNames() )
{
listBox.Items.Add( fileName );
}
}
}
Just putting them in your XAP as content won't deploy them to Isolated Storage.
On the first execution of your app, you'll need to copy the files to the Isolated Storage. If they're static, there's really no need to do this, as you can just access them as content. If you need to update them, then you will need to get them to the IsoStore.
Is there a way to launch a desktop application from a Metro-style app on Windows 8? I'm trying to create some simple shortcuts to desktop applications to replace the desktop icons on the start screen, which look out of place.
I just need something super simple, preferably in C#, to open an application as soon as the app loads. I'm planning on making these shortcuts for some games, photoshop, etc, not anything I've made myself. They're also just for personal use, so I can use direct paths to applications like "C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe"
If you simply want to run a desktop application like (notepad, wordpad, internet explorer etc) then go through Process Methods and ProcessStartInfo Class
try
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "C:\Path\To\App.exe";
p.Start();
}
// Exp 2
// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process.Start(startInfo);
startInfo.Arguments = "www.northwindtraders.com";
Process.Start(startInfo);
}
On Windows 8 Metro application i discovered this: How to Start a
external Program from Metro App.
All the Metro-style applications work in the highly sand boxed
environment and there is no way to directly start an external
application.
You can try to use Launcher class – depends on your need it may
provide you a feasible solution.
Check this:
Can I use Windows.System.Launcher.LauncherDefaultProgram(Uri) to invoke another metro style app?
Ref: How to launch a Desktop app from within a Metro app?
Metro IE is a special app. You cannot invoke an executable from Metro style apps.
Try this - I have not test yet but may be it will help you..
Launcher.LaunchFileAsync
// Path to the file in the app package to launch
string exeFile = #"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
I found a solution which is suitable for me. I just made an empty textfile in my app and called it launcher.yourappyouwanttostart and then executed it with
Windows.System.Launcher.LaunchFileAsync("launcher.yourappyouwanttostart");
On the first startup it asks you for the assocation for this file and then you choose the exe file you want to run and from now on every time you execute this file, your app will be started.
I haven't actually tried if it works and it's not really a beautiful solution, but I guess Metro-style apps can launch a URI.
You could then create a desktop-program that is registered for a custom URI scheme that would then do the actual program launching.
What you can do is host external WCF service on your computer with separate installation and connect to it from metro style application using localhost. Then you can do pretty much anything including Process.Start.
I love simple things, so my solution was to use this:
Process.Start("explorer", "shell:AppsFolder\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe!App")
This will start the "new" Sticky Notes coming with Anniversary Update to Windows 10, but it works with all other "Metro" apps I tested.
To find the name of the metro app, from Windows Explorer you have to find it in shell:appsfolder using the AppUserModelId column.