如何获取文件路径? [英] How to get file path?

查看:30
本文介绍了如何获取文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写食谱 Windows 项目,我准备了一些 .rtf 文件以将其加载到 RichTextBox 中.问题是文件的路径在任何计算机上都不能工作,只有我的.我使用的代码:

I'm working on cook book Windows project, and I prepared some .rtf files to load it in RichTextBox. The problem is that the path of the file can't work at any computer, just mine. The code I used:

richTextBox1.LoadFile("C:\\Users\\nardeen\\Desktop\\cookproject\\cookingproject\\italian.rtf", RichTextBoxStreamType.RichText);

另外我尝试过这个,但没有用:

In addition I tried with this but it didn't work:

string FilePath = System.Windows.Forms.Application.StartupPath + "\\italian.rtf";

richTextBox1.LoadFile(FilePath, RichTextBoxStreamType.RichText);

推荐答案

不要将 StartupPathCurrentDirectory 用于用户数据;这将根据您的应用程序的安装方式发生变化,标准用户将无法写入它,如果用户修复他们的应用程序,它可能会被删除.

Do not use StartupPath or CurrentDirectory for user data; that will change depending on how your application was installed, Standard users won't be able to write to it, and it will probably get deleted if the user repairs their application.

如果您尝试获取用户可以读写的文件夹的路径,并且在您卸载或修复应用程序时不会被删除,请使用

If you are trying to get the path to a folder that the user can read and write to, and that doesn't get deleted when you uninstall or repair the application, use

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    "cooking project", "Italian.rtf");

如果您想为用户包含一些入门数据,请将其作为资源或嵌入文件包含在您的项目中,并在安装应用程序时将其复制到用户文件夹中.如果您告诉我们您使用的安装程序技术,我们也可以提供相关建议.

If you want to include some starter data for users, include it in your project as a resource or embedded file, and copy it to the user folder when you install your application. If you tell us what installer technology you use, we can give advice on that as well.

这篇关于如何获取文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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