简体   繁体   中英

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.

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

Look at: 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 )

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:

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

Look at: Environment.SpecialFolder - a lot of useful places there.

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. (But folder like AppData that you said, are placed just in your solution)

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)

You can refer to the current directory using Environment class => Environment.CurrentDirectory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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