简体   繁体   English

创建和使用c#应用程序文件夹

[英]Creating and using c# Application Folder

我正在创建一个在我手动创建的文件夹中使用照片和XML文件的应用程序,我想让用户通过应用程序在运行时更新该文件夹的数据(添加照片和编辑Xml文件)我的问题是什么是最好的方法以及放置该文件夹的位置,我知道我必须放置相对路径所以我很困惑它是在AppData中如果是这样如何做到这一点。

// Use this to get the common directory
string CommonDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

// And combine it with your own. 
// On Window7 this will return "c:\ProgramData\YourCompany\YourProduct"
string YourDir = Path.Combine(CommonDir, @"YourCompany\YourProduct");

// And create the directory / ensure it exists
System.IO.Directory.CreateDirectory(YourDir);

There are other Special Folders you can get from the system, such as MyDocuments or Desktop as fits your needs best. 您可以从系统中获取其他特殊文件夹 ,例如MyDocumentsDesktop以满足您的最佳需求。

首先右键单击项目资源管理器并添加新文件夹,它会显示空文件夹并将此文件放入其中。

Look at: System.IO.IsolatedStorage 看看: System.IO.IsolatedStorage

You can manage your files using different scopes of isolation and don't bother about their actual place: 您可以使用不同的隔离范围管理文件,而不必担心其实际位置:

Application - Isolated storage scoped to the application. 应用程序 - 作用于应用程序的隔离存储。

Assembly - Isolated storage scoped to the identity of the assembly. 装配 - 作用于装配体标识的隔离存储。

Domain - Isolated storage scoped to the application domain identity. - 隔离应用程序域标识的隔离存储。

Machine - Isolated storage scoped to the machine. 机器 - 隔离存储作用于机器。

None - No isolated storage usage. - 没有孤立的存储空间使用。

Roaming - The isolated store can be placed in a location on the file system that might roam (if roaming user data is enabled on the underlying operating system). 漫游 - 隔离的存储可以放置在文件系统上可能漫游的位置(如果在底层操作系统上启用了漫游用户数据)。

User - Isolated storage scoped by user identity. 用户 - 按用户身份确定的隔离存储。

(from here: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragescope(v=vs.110).aspx ) (从这里: http//msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragescope (v= vs.110 ) .aspx

But possibly you don't need such a control over your files isolation. 但是,您可能不需要对文件隔离进行这样的控制。 In this case you can simply use local user's aplication folder: 在这种情况下,您只需使用本地用户的aplication文件夹:

string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

Look at: Environment.SpecialFolder - a lot of useful places there. 看看: Environment.SpecialFolder - 那里有很多有用的地方。

And, of course, you can always use you application's folder: 当然,您可以随时使用应用程序的文件夹:

AppDomain.CurrentDomain.BaseDirectory

Another ways of accessing it: Best way to get application folder path 访问它的另一种方法: 获取应用程序文件夹路径的最佳方法

You shouldn't place the folder in one of your Solution's folder like AppData (AppData is actually a folder, that can contain Database file) because when you finish the development of your application, you just get .EXE output file (That placed in Debug folder) and THAT IS the file that you use it or for example give it to your customer. 您不应该将文件夹放在解决方案的一个文件夹中,例如AppData(AppData实际上是一个文件夹,可以包含数据库文件),因为当您完成应用程序的开发时,您只需获得.EXE输出文件(放在Debug中)文件夹),这是您使用它的文件,或者例如将其提供给您的客户。 (But folder like AppData that you said, are placed just in your solution) (但是你说的AppData这样的文件夹只放在你的解决方案中)

So you can create the Folder anywhere you want. 因此,您可以随意创建文件夹。 But I offer you to create it in the current directory of your application. 但是我建议你在应用程序的当前目录中创建它。 (.EXE file) (.EXE文件)

You can refer to the current directory using Environment class => Environment.CurrentDirectory 您可以使用Environment class => Environment.CurrentDirectory来引用当前目录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM