写入 appdata 文件夹 - 权限问题?
我已经寻找过类似问题的一些答案,但它们并没有真正帮助我。
我有一个应用程序,我在其中嵌入了一些资源。启动时,应用程序会检查 appdata 文件夹中是否存在资源,如果不存在,则将模板文件从嵌入资源复制到 appdata 文件夹,然后加载它们,然后使用 appdata 文件夹中的模板文件作为工作副本。
我有一个帮助程序类,除其他外,它返回 appdata 和资源子文件夹,如下所示:
class Folders
{
static public String GetUserFolder()
{
return Application.LocalUserAppDataPath;
}
static public String GetResourcesFolder()
{
// If the resources folder does not exist then create it
String userFolder = GetUserFolder();
String resourcesFolder = userFolder + "\\Resources";
if (!Directory.Exists(resourcesFolder))
{
Directory.CreateDirectory(resourcesFolder);
}
return resourcesFolder;
}
...
因此,我的代码调用 GetResourcesFolder 方法,该方法返回路径(如果需要,在进程中创建文件夹)检查文件是否存在以及是否存在它不会尝试使用以下内容写入它:
String filename = Helpers.IO.Folders.GetResourcesFolder() + "\\data.dat";
FileStream outFile = System.IO.File.OpenWrite(filename);
所以我已经设置了场景,并且该代码正在我开发办公室的所有机器上运行。然而,一些异地同事抱怨说,它在他们的机器上“崩溃”了——在每种情况下都是 XP 机器——但除此之外,他们没有返回很多有用的信息——正在努力从他们那里获取更多信息。我办公室里有 XP 机器,它运行起来没有任何问题。
在挖掘出一些不久前“存档”的非常旧的开发机器后,我也成功地在两台 xp (sp2) 机器上发生了崩溃。在这两种情况下,崩溃似乎都与写入权限有关,并且使用“运行方式...”运行应用程序已经解决了问题并且可以正确执行。但是,一旦应用程序成功运行,一旦应用程序不再崩溃,即使我删除了它从应用程序数据文件夹创建的文件/文件夹,即使我不提升权限,它仍然会在后续执行中成功创建文件/文件夹。
我遇到的问题是,我现在不能再在任何可用的开发机器上重复崩溃,而且我不知道如何将机器恢复到可以的状态。
任何人都知道可能导致问题的原因,或者我如何能够将机器恢复到“原始”状态,以便能够重复崩溃并帮助我追踪它。
I've had a scout around some answers to similar questions but they haven't really helped me much.
I have an app, into which I've embedded some resources. At launch the app checks to see if the resources exist in the appdata folder and if not copies the template files from the embedded resources to the appdata folder before loading them and then using the ones in the appdata folder as the working copies.
I have a helper class which amongst other things returns the appdata and resources subfolder as follows:
class Folders
{
static public String GetUserFolder()
{
return Application.LocalUserAppDataPath;
}
static public String GetResourcesFolder()
{
// If the resources folder does not exist then create it
String userFolder = GetUserFolder();
String resourcesFolder = userFolder + "\\Resources";
if (!Directory.Exists(resourcesFolder))
{
Directory.CreateDirectory(resourcesFolder);
}
return resourcesFolder;
}
...
So my code calls the GetResourcesFolder method which returns the path (creating the folder in the process if it needs to) checks to see if the file exists and if it doesn't tries to write to it using something like:
String filename = Helpers.IO.Folders.GetResourcesFolder() + "\\data.dat";
FileStream outFile = System.IO.File.OpenWrite(filename);
So I've set the scene and this code is working on all the machines I had in the development office. However a couple of off site colleagues have complained it "crashes" on their machines - in each case an XP machine - but otherwise not a lot of useful information coming back from them - working on trying to get something more informative from them. I have XP machines in the office that it has worked on without problems.
After digging out some really old dev machines that were "archived" a while ago, I've managed to have a crash on two xp (sp2) machines also. On both occasions the crash seems to be related to write permissions and running the app using "Run As..." has resolved the problem and it executes correctly. However once the app has been successfully run once the app no longer crashes, even if I delete the files/folders it created from the appdata folder it will still create the successfully on subsequent executions even if I don't elevate permissions.
The problem I have is that I can now no longer repeat the crash on any dev machines available to me and I don't know how to go about putting the machine back into the state where I can.
Anybody got any ideas on what might be causing the problem, or how I may be able to return the machines to a "virgin" state to be able repeat the crash and help me track it down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种做法是创建 XP 虚拟机。您可以在安装前保存机器的状态以进行测试。安装后,只需恢复到之前的状态即可再次测试。有一些供应商提供免费虚拟机:
http://www.microsoft.com/windows/ virtual-pc/
https://www.virtualbox.org/
相关问题本身也是如此,我不知道更好的比在虚拟机上安装 VS 进行测试的方式。
One course of action is to create a Virtual Machine of XP. You can save the state of the machine before install for testing. After your install just revert back to the previous state to test again. There are a few Vendors with free Virtual Machines:
http://www.microsoft.com/windows/virtual-pc/
https://www.virtualbox.org/
As too the related problem itself, I don't know a better way than to install VS on a virtual machine for testing purposes.
+1 给 Erik 用于重现问题的 VM 解决方案。
要追踪权限问题,请考虑使用 ProcMon ( http://technet.microsoft.com/ en-us/sysinternals/bb896645.aspx ) - 将显示您想要的(而不是:))有关进程进行的文件/注册表访问的所有内容。我建议首先在您的程序运行良好的机器上尝试几次,以便为您的流程设置过滤设置并了解应该发生什么。
+1 to Erik's VM solutions for reporducing the issue.
For tracking down permissions issues consider using ProcMon ( http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx ) - will show enverything you ever wanted (and not :) ) about file/regisstry access made by a process. I'd recommend trying it several times first on machine where you program works fine to get filtering setup for your process and get a sense of what should be happening.