简体   繁体   English

拒绝访问此目录

[英]Access to this directory is denied

I have discovered a problem with using a StreamReader to read a text file. 我发现使用StreamReader读取文本文件有问题。 If you use it, it somehow renders the directory the file itself is located unmovable. 如果你使用它,它会以某种方式呈现文件本身所在的目录不可移动。 For example- 例如-

cuLocation = "C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt";
System.IO.StreamReader objReader = new System.IO.StreamReader(cuLocation);
currentUser = objReader.ReadLine();
TBcurrentUser.Text = "The current user is " + currentUser + ".";

All this happens upon form load. 所有这些都发生在表单加载时。 Then I have a button click event set up where this happens- 然后我有一个按钮点击事件设置,发生这种情况 -

System.IO.Directory.Move("C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft", "C:\\Users\\WoopyCat\\AppData\\Roaming\\.MCSwitcher\\" + currentUser);

However, the IDE says access to .minecraft is denied. 但是,IDE表示拒绝访问.minecraft。 But if I remove this code- 但如果我删除此代码 -

cuLocation = "C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt";
System.IO.StreamReader objReader = new System.IO.StreamReader(cuLocation);
currentUser = objReader.ReadLine();

And replace it with this code- 并用此代码替换它 -

currentUser = "Paul";

It works perfectly. 它完美地运作。 It can access .minecraft. 它可以访问.minecraft。 But I need to read the currentUser.txt file in order for my program to work. 但我需要读取currentUser.txt文件才能使我的程序正常工作。 Any help? 有帮助吗?

Again, to reiterate- this code- 再次重申 - 这段代码 -

cuLocation = "C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft\\currentUser.txt";
System.IO.StreamReader objReader = new System.IO.StreamReader(cuLocation);
currentUser = objReader.ReadLine();

Prevents this code from working- 防止此代码工作 -

 System.IO.Directory.Move("C:\\Users\\WoopyCat\\AppData\\Roaming\\.minecraft", "C:\\Users\\WoopyCat\\AppData\\Roaming\\.MCSwitcher\\" + currentUser);

You do close obj.Reader before the move using objReader.Close() , right? 你在使用objReader.Close()移动之前关闭obj.Reader,对吗? Having a stream opened to the file will prevent it being moved. 将流打开到文件将阻止它被移动。 If that isnt the case, theres an application (handle I think it's called) that will let you see what has access to a file, so you can see whats preventing you from moving the dir. 如果不是这样的话,那么一个应用程序(我认为它被调用的句柄)可以让你看到什么可以访问文件,所以你可以看到什么阻止你移动目录。

The only possible reason I can think of is you are not really the WoopyCat user, here is the correct way to get the path based of the currently running user. 我能想到的唯一可能的原因是你不是真正的WoopyCat用户,这是获取当前正在运行的用户的路径的正确方法。

var roamingFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); //This equals the \AppData\Roaming\ folder for the current user
Directory.Move(Path.Combine(roamingFolder, ".minecraft"), Path.Combine(roamingFolder,currentUser));

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

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