在 Monotouch 中写入文件 [英] Write to a File in Monotouch

查看:14
本文介绍了在 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?).

推荐答案

[注意:我的回答非常彻底,因为我不知道您对 app bundles 的理解程度或您的应用程序的结构iPhone 应用程序的沙盒小世界 - 抱歉,如果我涵盖了你已经知道的东西 - 我更喜欢写得太多而不是太少,并在讨论 如何时添加一点为什么/em>...]

[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 应用程序的超级沙盒化程度,您无法访问在进行桌面开发时通常可以使用的共享文件夹(例如,一个位于用户目录下的公共 Documents 文件夹)应用程序有权访问的主文件夹).

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:

~/Library/Application Support/iPhone Simulator

在其中,有一个 User/Applications 文件夹,其中包含您安装到模拟器的应用程序.深入到这些文件夹中的任何一个,您就可以看到您的应用可以在手机上访问的文件夹结构.

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 文件夹是最佳选择.这不是您创建文件的唯一选择,但它是这项工作的正确选择.除了正确存储您的文件之外,将它们保存在 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);

如果您想测试一下,这是一些非常简单的代码,它会将一个名为out.txt"的文件写入您应用的 Documents 文件夹.此代码还读取文件的内容以显示它已创建 - 为了进一步验证,请转到模拟器的应用程序文件夹,按修改日期对应用程序文件夹进行排序,深入查看最近修改的内容,并查看其文档内部文件夹 - 您会找到out.txt"(您无法按名称找到您的应用程序文件夹,因为当您的应用程序安装时,它会被塞进一个名为2B3CA854-FADB-4DDC-9732-0E61B3DD8D8C"的文件夹中" - 按修改日期对文件夹进行排序将指向最近修改的应用程序,在这种情况下,它是包含以下代码的任何应用程序):

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"映射到您的应用程序的 Documents 文件夹.除此之外,它像往常一样是 .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天全站免登陆