I have an application which I have written in MonoTouch. I dropped a file called ARCSDev into the applications folder and included it in the project, I then tried to read in the file using the following code:
private void generateChart (int chartNumber, string palette)
{
string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "ARCSDev");
Loader loader = new Loader (filePath);
However my application can't seem to find the file, can anyone explain what I'm doing wrong?
From a quick look you're trying to load from the "Documents" directory (i.e. the one you have read-write access, could access via iTunes and get backed up), while your own file is inside the application directory (which is read-only).
What you want is (likely) NSBundle.MainBundle.BundlePath
This Xamarin's article covers this (and a lot more) about the iOS file system.
Related
I have made a c# app to access files that was in the Recent Files. But i need to open the file when clicked. How can i do it?
Code : https://imgur.com/a/rSfSRrx
NB: My app shows files that has been used by the user recently. The name of the file he used will be showed when user switches on the pc next time. So that he can access it quickly and easily. I have accessed the Recently Used Files in windows and retrieved the file name. But I don't know about the code to open that file. That's what I am asking. File name is directly retrieved from the recent files.
Ideally you should be using the IShellFolder interface to enumerate and execute items in the Recent folder but I assume you are not going to do that and instead keep the code you already have.
Environment.SpecialFolder.Recent is indeed the folder where these files are kept. These .lnk files are shortcuts and when you want to execute them you must make sure you have a full path (Environment.SpecialFolder.Recent + filename + ".lnk") and pass this to Process.Start:
using System.Diagnostics;
...
Process myProcess = new Process()
myProcess.StartInfo.UseShellExecute = true;
myProcess.StartInfo.FileName = fullpathtoshortcut;
myProcess.Start();
If you want to be a good citizen and do this properly with the shell interfaces you probably need to get the shell API Code Pack.
edit: About the path-problem. I will try to get there later. For now I just need help for zip a file. Could not find a way to do this yet.
Im currently going through a few basics and I don't know what I have to look for to get to where I want to be. So, what are my goals?:
I want to create a name.json file here C:\Users\Username\Desktop
Then I want to compress name.json to an zip file.
I also created another file Testfile.zip on my Desktop. I want to unzip that file.
So far I created a name.json file. But I cannot find a solution on how to create one on the desktop.
I could not find a solution on compressing name.json so far.
public MainPage()
{
this.InitializeComponent();
createJson();
UnzipFile();
}
public async void createJson()
{
string text = "This text";
string json = JsonConvert.SerializeObject(text);
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("name.json");
await FileIO.WriteTextAsync(file, json);
}
public async void UnzipFile()
{
var localFolder = ApplicationData.Current.LocalFolder;
var archive = await localFolder.GetFileAsync("Testfile.zip");
ZipFile.ExtractToDirectory(archive.Path, localFolder.Path);
}
Working with .zip files is possible using System.IO.Compression.ZipArchive and related classes. You are already using ZipFile.ExtractToDirectory which is the right approach here. However, the issue you are facing is rather related to permissions.
UWP apps are sandboxed which means they are not allowed to touch any filesystem location by default. This is to ensure better security and easier uninstallation of apps. This however means you cannot easily create a file on the user's desktop, as your app does not have access there. The only folders your app can freely access are those accessible by ApplicationData.Current and then those it declares access to in application manifest. You can also declare broad filesystem access here to get access to all locations on the PC.
To further complicate this, there are two types of I/O APIs in UWP. The modern StorageFile API which is async enabled, but tad slower, and the classic file I/O APIs in C# which includes ZipFile and ZipArchive. The main disadvantage of the classic APIs is that they always have access only to application folders and you can never access any other system paths, even if you declare broad filesystem access.
However, even without declaring broad filesystem access capability you can manually get access to the folder/file of user's choosing using FolderPicker, FileOpenPicker and FileSavePicker. Using these you can let the user choose the destination where you will save the file or open a file.
Finally - to circumvent the limitation of not being able to use the classic file I/O APIs, you can first unzip the .zip file in a temporary folder inside ApplicationData.Current.LocalFolder and then use the StorageFile.MoveAndReplaceAsync(IStorageFile) or StorageFile.MoveAsync(IStorageFolder) method to move the files to the location the user has chosen using FileSavePicker.
For further info you can check out this blog post with a tutorial on using .zip in UWP.
I'm building a repository folder and file structure with many dependencies in version control using .NET for our data warehouse. Currently I have code to create dummy files and folders with C# code (see below). However, there are objects being shared. So I'd like to create shortcuts to Windows files and shortcuts to Windows folders as well as files. What would the code look like in C# to accomplish this?
Create Folder via C#.NET code:
string activeDir = #"C:\Source\EDW\dw-objects\trunk\table-objects";
string objectName = reader[0].ToString();
string newPath = System.IO.Path.Combine(activeDir, objectName);
System.IO.Directory.CreateDirectory(newPath);
Code varies depending on files based on format
Please use the ShellClass to create shortcuts also you will
need to get the special directory from desktop using Environment.SpecialFolder.DesktopDirectory
A very good example showing step by step can be found here http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App
Have a look at This Article it shows you how to manipulate NTFS Juncture points using c#. Because you will want to access the folders you need to have juncture points instead of shrotcuts since one is followed by machine processing and one isn't.
i am new to c#. if i have a file in ProjectFolder\file.txt, how can i access it in code. eg. i want to read a file in code. i remember the path looks like application://.../file.txt but i cant remember how exactly
Just set your Copy to Output Directory property of your file to Copy if newer:
When you run your program from the output directory, you can simply open the file from the current directory:
string text = File.ReadAllText("file1.txt");
Most people's solutions refer to accessing a file on your file system. However, you mentioned application local URIs, which made me think you are talking about embedded resources. Those resources aren't a separate file on your filesystem. So, the rest of my post is about that.
If you would like to include the file in your assembly, you have to change the properties on the file, changing the BuildAction property to Embedded Resource.
From there, you can access it with the resource path, as you described (see Pack URIs: http://msdn.microsoft.com/en-us/library/aa970069.aspx), or you can access it this way:
var stream = System.Reflection.Assembly.GetExecutingAssembly()
.GetManifestResourceStream("ConsoleApplication1.blah.txt");
var reader = new StreamReader(stream);
Console.WriteLine(reader.ReadToEnd());
How would I create and write to a file in a Monotouch iPhone app?
The file should persist between application launches, so I guess it has to be placed somewhere in the App bundle ( documents or resources?).
[Note: My response is pretty thorough because I don't know your level of understanding regarding app bundles or the structure of your iPhone app's sandboxed little world - apologies if I cover things you already know - I prefer to write a little too much than too little, and to add a bit of the why when discussing the how...]
You have a few options (of course). I'm assuming you're already familiar with .Net to some extent and that your question is more about how to do this the iPhone Way.
Every iPhone app (and you'll see the same thing for apps on OS X) is a "bundle" which isn't an executable in the traditional sense, but actually a folder hierarchy inside of which your app binary lives (along with resources, settings, etc.).
Because of how uber-sandboxed iPhone apps are, you don't have access to the shared folders you'd usually be able to use when doing desktop development (having, for example, a common Documents folder that lives under a user's home folder to which applications have access).
Instead, your app has its own folder hierarchy that's like its own personal set of the folders that would typically be shared across apps.
The easiest way to see what your app's folder structure looks like on the phone is to look at the folder the iPhone simulator uses for app installs, settings, blah blah blah. On my machine (I don't recall if this is configurable, but it's probably the same on your system), you can get to the folder by this path:
~/Library/Application Support/iPhone Simulator
Inside of that, there's a User/Applications folder that contains the apps you've installed to the simulator. Drill down into any one of those folders, and you can see the folder structure your app will have access to on the phone.
For storing files that you'd like persisted across app sessions, your app's Documents folder is the spot. It's not your only choice for creating files, but it's the right choice for this job. In addition to your files being properly stored, keeping them in the Documents folder will also get them backed up by iTunes when the user syncs.
With MonoTouch, you can get your app's Documents folder path with Environment.GetFolderPath(Environment.SpecialFolder.Personal);
If you'd like to test it out, this is some extremely simple code that'll write a file called "out.txt" to your app's Documents folder. This code also reads the contents of the file to show it was created - for further verification, go to the simulator's Applications folder, sort the app folders by the date they were modified, drill down into the most recently modified, and look inside its Documents folder - you'll find "out.txt" (you can't find your app's folder by name because, when your app is installed, it gets stuffed inside a folder with a name like "2B3CA854-FADB-4DDC-9732-0E61B3DD8D8C" - sorting the folders by the date they were modified will point you to the most recently modified app, which, in this case, is whatever app contains the following code):
// For this to function, don't forget "using System.IO;"
// If you're just playing around with this to see it work, place it inside
// your AppDelegate's "FinishedLaunching" method in main.cs
string path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
string filePath = Path.Combine(path, "out.txt");
// File.WriteAllText will create a file and then write text to it. If the
// file already exists, File.WriteAllText will overwrite it.
File.WriteAllText(filePath, "Howdy, world.");
// Now we prove it worked by reading the contents of the file and then
// printing them to the console...
string text = File.ReadAllText(filePath);
Console.WriteLine(text);
So, the only thing here that's really iPhone-specific is knowing that "Environment.SpecialFolder.Personal" maps to your app's Documents folder. Beyond that, it's .Net as usual.
And, again, this was probably overkill, but I wanted to answer sufficiently thoroughly for everybody who sees it.
The following How To from the Xamarin.iOS guide site has a few pointers to where to store your files:
http://docs.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
You can do something like this at runtime:
using (StreamWriter writer = new StreamWriter (Path.Combine (Environment.SpecialFolders.Documents, "yourfilename.ext"))) { }
and that will create the file. To open it for reading, use the same Path.Combine() but with StreamReader.