简体   繁体   English

无论用户如何移动文件(C#)

[英]How Can I Move a File No Matter The User (C#)

My Problem is that I'm trying to make a Windows Form Application and in I have to move files around Problem with that is that to move files I need to move within their user directory, meaning我的问题是我正在尝试制作 Windows 表单应用程序并且我必须移动文件问题是要移动文件我需要在他们的用户目录中移动,这意味着

C:\Users"UserName" C:\Users"用户名"

I just need to know how to find the "UserName" or how I can move files Within their "UserName" Directory without having to make change the code for every person我只需要知道如何找到“用户名”或如何在他们的“用户名”目录中移动文件,而无需为每个人更改代码

hope you get what I mean希望你明白我的意思

You can use Environment.GetFolderPath() and Environment.SpecialFolder.UserProfile to get the current user profile path, like so:您可以使用Environment.GetFolderPath()Environment.SpecialFolder.UserProfile获取当前用户配置文件路径,如下所示:

var userDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

But be aware that this isn't always a good idea, especially if you're dealing with Roaming User Profiles.但请注意,这并不总是一个好主意,尤其是在您处理漫游用户配置文件时。

A roaming user works on more than one computer on a network.漫游用户在网络上的多台计算机上工作。 A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.漫游用户的配置文件保存在网络上的服务器上,并在用户登录时加载到系统上。

In that case the Microsoft recommended place to store application specific data is the ApplicationData directory, which you can access like this:在这种情况下,Microsoft 推荐的存储应用程序特定数据的位置是ApplicationData目录,您可以像这样访问它:

var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

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

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