免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6866 | 回复: 8
打印 上一主题 下一主题

[C] C语言修改文件属性 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-03 13:11 |只看该作者 |倒序浏览
请教一下,用c语言怎样把一个文件的隐藏属性去掉?

论坛徽章:
0
2 [报告]
发表于 2007-07-03 13:21 |只看该作者
什么系统下的文件?

论坛徽章:
0
3 [报告]
发表于 2007-07-03 13:24 |只看该作者
有隐藏属性的,估计是M$家族的了吧

论坛徽章:
0
4 [报告]
发表于 2007-07-03 13:48 |只看该作者
windows系统下的文件

论坛徽章:
0
5 [报告]
发表于 2007-07-03 15:20 |只看该作者
自己顶

论坛徽章:
0
6 [报告]
发表于 2007-07-03 15:22 |只看该作者
C 语言不具备这个功能,除非是某个 C 方言,但集成到如此程度的我还不曾听说。如此说来,要完成这个工作,只能寻求 Microsoft Platform SDK 了,敬请留意。

论坛徽章:
0
7 [报告]
发表于 2007-07-03 15:29 |只看该作者
查MSDN SetFileAttributes 函数。

论坛徽章:
0
8 [报告]
发表于 2007-07-05 12:33 |只看该作者
#include <Windows.h>
#include <stdio.h>
#include <strsafe.h>
void _tmain(int argc, _TCHAR* argv[])
{
   WIN32_FIND_DATA FileData;
   HANDLE hSearch;
   DWORD dwAttrs;   
   TCHAR szDirPath[] = TEXT("c:\\TextRO\\");
   TCHAR szNewPath[MAX_PATH];   

   BOOL fFinished = FALSE;

// Create a new directory.

   if (!CreateDirectory(szDirPath, NULL))
   {
      printf("Could not create new directory.\n");
      return;
   }

// Start searching for text files in the current directory.

   hSearch = FindFirstFile(TEXT("*.txt"), &FileData);
   if (hSearch == INVALID_HANDLE_VALUE)
   {
      printf("No text files found.\n");
      return;
   }

// Copy each .TXT file to the new directory
// and change it to read only, if not already.

   while (!fFinished)
   {
      StringCchCopy(szNewPath, MAX_PATH, szDirPath);
      StringCchCat(szNewPath, MAX_PATH, FileData.cFileName);
      if (CopyFile(FileData.cFileName, szNewPath, FALSE))
      {
         dwAttrs = GetFileAttributes(FileData.cFileName);
         if (dwAttrs==INVALID_FILE_ATTRIBUTES) return;

         if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
         {
            SetFileAttributes(szNewPath,
                dwAttrs | FILE_ATTRIBUTE_READONLY);
         }
      }
      else
      {
         printf("Could not copy file.\n");
         return;
      }

      if (!FindNextFile(hSearch, &FileData))
      {
         if (GetLastError() == ERROR_NO_MORE_FILES)
         {
            printf("Copied all text files.\n");
            fFinished = TRUE;
         }
         else
         {
            printf("Could not find next file.\n");
            return;
         }
      }
   }

// Close the search handle.

   FindClose(hSearch);
}

论坛徽章:
0
9 [报告]
发表于 2007-07-05 12:36 |只看该作者
#include <stdlib.h>
int main()
{
       system("attrib file -h");
       return 0;
}

前提是不能是系统文件。哈哈!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP