문제

나는 원인을 일으키는 깃발을 설정하려고합니다 Read Only 당신이 나타날 때 확인란을 확인하십시오 right click \ Properties 파일에.

감사!

도움이 되었습니까?

해결책

두 가지 방법:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
fileInfo.IsReadOnly = true/false;

또는

// Careful! This will clear other file flags e.g. FileAttributes.Hidden
File.SetAttributes(filePath, FileAttributes.ReadOnly/FileAttributes.Normal);

FileInfo의 IsReadOnly 속성은 본질적으로 두 번째 방법에서 수동으로 수행 해야하는 비트 플리핑을 수행합니다.

다른 팁

에게 세트 사실상 파일을 작성할 수없는 읽기 전용 플래그 :

File.SetAttributes(filePath,
    File.GetAttributes(filePath) | FileAttributes.ReadOnly);

에게 제거하다 사실상 파일을 쓸 수있는 읽기 전용 플래그 :

File.SetAttributes(filePath,
    File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);

에게 비녀장 읽기 전용 플래그는 지금 당장과 반대입니다.

File.SetAttributes(filePath,
    File.GetAttributes(filePath) ^ FileAttributes.ReadOnly);

이것은 기본적으로 비트 마스크입니다. 읽기 전용 플래그를 설정하기 위해 특정 비트를 설정하면 플래그를 제거하도록 지우 셨습니다.

위의 코드는 파일의 다른 속성을 변경하지 않습니다. 다시 말해, 위의 코드를 실행하기 전에 파일이 숨겨져 있다면 나중에 숨겨져 있습니다. 파일 속성을 간단히 설정 한 경우 .Normal 또는 .ReadOnly 과정에서 다른 깃발을 잃게 될 수 있습니다.

씨# :

file.setattributes (filepath, fileattributes.normal);

file.setAttributes (filepath, fileattributes.ReadOnly);

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top