写在MonoTouch的文件 [英] Write to a File in Monotouch

查看:122
本文介绍了写在MonoTouch的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建并写入到一个MonoTouch的iPhone应用程序的文件?

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?).

推荐答案

[注意:我的反应是pretty的彻底的,因为我不知道你了解有关的应用程序包或水平你的iPhone应用程序的沙盒中的小世界的结构 - 道歉,如果我盖的东西你已经知道了 - 我preFER写的不是太少有点太多,并添加了一下的为什么的讨论时中的如何 ...]

[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...]

您有几种选择(当然)。我假设你已经很熟悉的.Net在一定程度上与你的问题更多的是如何做到这一点的iPhone之路。

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.

每一个iPhone应用程序(你会看到同样的事情在OS X的应用程序)是一种捆绑,这是不是传统意义上的可执行文件,但实际上是一个文件夹层次结构,你的应用程序二进制的生活里(随着资源,设置等)。

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

由于如何尤伯杯沙盒iPhone应用程序是,你没有访问共享文件夹,你通常会能够使用做桌面开发(其时,例如,常见的文档文件夹中,生活在用户的主文件夹的应用程序可以访问)。

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.

要看到你的应用程序的文件夹结构看起来像手机看iPhone模拟器使用的应用程序文件夹最简单的方法安装,设置,等等等等。在我的机器(我不记得,如果这是可配置的,但它可能是您的系统上一样),你可以通过这条路径得到的文件夹:

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:

〜/库/应用程序支持/ iPhone模拟器

里面的那个,还有包含您已经安装到模拟器的应用程序用户/应用程序文件夹中。向下钻取到这些文件夹中的任何一个,你可以看到该文件夹​​结构,你的应用程序将有机会获得在手机上。

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.

有关存储,你想跨应用程序会话持久化的文件,你的应用程序的文档文件夹是现货。这不是你创建文件的唯一选择,但它是这项工作的的右键的选择。除了你的文件被妥善保存,让他们在Documents文件夹中也将获得他们支持由iTunes的当用户同步。

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.

使用MonoTouch的,你可以得到你的应用程序的文件与 Environment.GetFolderPath(Environment.SpecialFolder.Personal)文件夹的路径;

With MonoTouch, you can get your app's Documents folder path with Environment.GetFolderPath(Environment.SpecialFolder.Personal);

如果你想测试一下,这是一些非常简单的code表示将编写一个名为out.txt到你的应用程序的文档文件夹中的文件。这code也读取文件的内容,以显示它的创建 - 为进一步核实,转到模拟器的应用程序文件夹中,他们被修改的日期应用程序文件夹进行排序,深入到最近修改,并期待里面的文档文件夹 - 你会发现out.txt(你找不到的名字您的应用程序的文件夹,因为,安装了您的应用程序时,它就会被塞进一个文件夹里面有一个名称,如2B3CA854-是fadb-4DDC- 9732-0E61B3DD8D8C - 由他们进行了修改,将指向你最近修改的应用程序,其中,在这种情况下,无论是应用程序包含以下code)该日期排序文件夹:

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

所以,这里的唯一的事情是真的iPhone的具体是知道Environment.SpecialFolder.Personal映射到您的应用程序的文件夹。除此之外,它的.Net和往常一样。

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 - hope this helps :)

这篇关于写在MonoTouch的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆