C ++ CreateDirectory使文件夹变为只读 [英] C++ CreateDirectory makes folder read-only

查看:266
本文介绍了C ++ CreateDirectory使文件夹变为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事一个涉及解析可定制文件夹中的一堆xml文件的项目.
为了节省您几百行代码...
我在Windows XP上使用MFC来构建对话框应用程序,CString是存储字符串数据的主要方法,所有全局应用程序设置都在静态类实例AppSettings中(适当).问题是,用户定义的文件夹存储在CString中,然后我需要获取该基本文件夹,在其中添加一个子文件夹,然后在该子文件夹中创建一个文件,或者如果存在则覆盖它.
到目前为止,我已经完成了文件写入工作.除了创建子文件夹外,问题还在于当我尝试写入新创建的文件夹中的文件时,我会崩溃.检查后,我使用CreateDirectory()创建文件夹,并将只读属性放在文件夹上.最终用户不必在每次添加条目(创建子文件夹)时都必须更改所有这些权限.因此,我该如何A)创建文件夹后,将其权限更改为可写..或B)在创建过程中设置新文件夹的权限.现在我正在这样做:

I am working on a project that involves parsing a bunch of xml files in a customizable folder.
For sake of saving you several hundred lines of code...
Im using MFC on windows XP to build a dialog app, CString is my primary means of storing string data, all global app settings are in a static class instance AppSettings (appropriately). The problem is, that the user defined folder is stored in a CString, I then need to take that base folder, append a sub folder to it, and create a file in that subfolder, or overwrite it if it exists.
So far I have the writing to file done. As well as creating the subfolder, problem is that when i try to write to a file in the newly created folder, I get crashes. Upon examining, I use CreateDirectory() to create the folder, and it puts the read-only attribute on the folder. The end user is not going to want to have to change all these permissions each time they add an entry (which creates a subfolder). So how do I either A) after creating the folder, change its permissions to writable..or B) set the permisions of the new folder during creation. Right now Im doing this:

folderpath.Format(L"%s\\%s_%s_%i", 
App_Settings.ClientFolder.GetString(), // client folder
                                m_Clients.at(i).getLastName().GetString(), // first name
                                m_Clients.at(i).getFirstName().GetString(), // last name
                                m_Clients.at(i).getID()); // id

// check if directory exists
DWORD res = GetFileAttributes(folderpath.GetString());
if (res == INVALID_FILE_ATTRIBUTES)
{
	// need to create the directory
	if (!CreateDirectory(folderpath.GetString(),NULL))
	{
		// failed to create directory
		return false;
	}
	// make sure folder is writeable
	// ?
}


这是一个循环,循环遍历引导程序列表中的每个客户端,并为每个客户端创建一个文件夹,然后将客户端数据写入每个相应目录中的文件中.
我认为这与将LPSECURITY_ATTRIBUTES设置为NULL有关.但是该结构的文档对我没有用. Google也会提供有关CreateDirectory的最少相关信息.在这一点上的任何帮助,将不胜感激.谢谢


This is in a loop that counts through each of the clients in a vector list, and creates a folder for each one, then writes client data to a file in each respective directory.
Im thinking that it has something to do with LPSECURITY_ATTRIBUTES being set to NULL. But the documentation for the structure is useless to me. Google turns up minimal relevant information about CreateDirectory as well. Any help at this point would be appreciated. Thanks

推荐答案

我尝试在Windows 7上重新创建您的问题,并且能够创建新目录,向该目录添加文件并写入数据.到该文件中. Windows资源管理器将目录显示为只读,但我怀疑这不是绝对正确.您声明自己的程序崩溃了,但是您确定它们是此操作的结果吗?尝试使用调试器逐步检查代码,以查看是否可以准确确定崩溃发生的位置.
I have tried to re-create your problem on Windows 7, and am able to create a new directory, add a file to that directory, and write data into that file. Windows explorer shows the directory as read only but I suspect that this is not absolutely correct. You state that you get crashes in your program, but are you sure they are the result of this action? Try stepping through the code with the debugger to see if you can identify exactly where the crash occurs.


目录始终被视为只读目录(在内部它类似于读取目录) -仅在您想到的情况下才输入文件).正如Richard所暗示的那样,您的错误在其他地方.
A directory is always treated as read-only (internally it''s sorta like a read-only file entry if you think of it). As Richard hinted already, your error is somewhere else.


这篇关于C ++ CreateDirectory使文件夹变为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆