Answered by:

C# File permissions question

Question

While creating a file synchronization program in C# I tried to make a method 'copy' from LocalFileItem class that uses sing System.IO.File.Copy(destination.Path, Path, true) where Path is a string.
After executing this code with destination.Path = "C:\\Test2" and this.Path = "C:\\Test\\F1.txt" I get an exception saying that I do not have the required file permissions to do this operation on C:\Test, but C:\Test is owned by myself(the current user). Does anybody knows what is going on, or how to get around this?

Here is the code:

1

using System;

2

using System.Collections.Generic;

3

using System.Text;

4

using System.IO;

5

6

7

namespace Diones.Util.IO

8

{

9

/// <summary>

10

/// An object representation of a file or directory.

11

/// </summary>

12

publicabstractclass FileItem : IComparable

13

14

{

15

protected String path;

16

public String Path

17

{

18

set { this.path = value; }

19

get { returnthis.path; }

20

}

21

protectedbool isDirectory;

22

publicbool IsDirectory

23

{

24

set { this.isDirectory = value; }

25

get { returnthis.isDirectory; }

26

}

27

/// <summary>

28

/// Delete this fileItem.

29

/// </summary>

30

publicabstractvoid delete();

31

/// <summary>

32

/// Delete this directory and all of its elements.

33

/// </summary>

34

protectedabstractvoid deleteRecursive();

35

/// <summary>

36

/// Copy this fileItem to the destination directory.

37

/// </summary>

38

publicabstractvoid copy(FileItem fileD);

39

/// <summary>

40

/// Copy this directory and all of its elements

41

/// to the destination directory.

42

/// </summary>

43

protectedabstractvoid copyRecursive(FileItem fileD);

44

/// <summary>

45

/// Creates a FileItem from a string path.

46

/// </summary>

47

/// <param name="path"></param>

48

public FileItem(String path)

49

{

50

Path = path;

51

if (path.EndsWith("\\") || path.EndsWith("/")) IsDirectory = true;

52

else IsDirectory = false;

53

}

54

/// <summary>

55

/// Creates a FileItem from a FileSource directory.

56

/// </summary>

57

/// <param name="directory"></param>

58

public FileItem(FileSource directory)

59

{

60

Path = directory.Path;

61

}

62

publicoverride String ToString()

63

{

64

return Path;

65

}

66

publicabstractint CompareTo(object b);

67

}

68

/// <summary>

69

/// A file or directory on the hard disk

70

/// </summary>

71

publicclass LocalFileItem : FileItem

72

{

73

publicoverridevoid delete()

74

{

75

if (!IsDirectory) File.Delete(this.Path);

76

else deleteRecursive();

77

}

78

protectedoverridevoid deleteRecursive()

79

{

80

Directory.Delete(Path, true);

81

}

82

publicoverridevoid copy(FileItem destination)

83

{

84

if (!IsDirectory) File.Copy(destination.Path, Path, true);

85

else copyRecursive(destination);

86

}

87

protectedoverridevoid copyRecursive(FileItem destination)

88

{

89

Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(

90

Path, destination.Path, true);

91

}

92

/// <summary>

93

/// Create's a LocalFileItem from a string path

94

/// </summary>

95

/// <param name="path"></param>

96

public LocalFileItem(String path)

97

: base(path)

98

{

99

}

100

/// <summary>

101

/// Creates a LocalFileItem from a FileSource path

102

/// </summary>

103

/// <param name="path"></param>

104

public LocalFileItem(FileSource path)

105

: base(path)

106

{

107

}

108

publicoverrideint CompareTo(object obj)

109

{

110

if (obj is FileItem)

111

{

112

FileItem fi = (FileItem)obj;

113

if (File.GetCreationTime(this.Path).CompareTo

114

(File.GetCreationTime(fi.Path)) > 0) return 1;

115

elseif (File.GetCreationTime(this.Path).CompareTo

116

(File.GetCreationTime(fi.Path)) < 0) return -1;

117

else

118

{

119

if (File.GetLastWriteTime(this.Path).CompareTo

120

(File.GetLastWriteTime(fi.Path)) < 0) return -1;

121

elseif (File.GetLastWriteTime(this.Path).CompareTo

122

(File.GetLastWriteTime(fi.Path)) > 0) return 1;

123

elsereturn 0;

124

}

125

}

126

else

127

thrownew ArgumentException("obj isn't a FileItem");

128

}

129

}

130

}

131

This signature cannot be displayed because it violates Microsoft's intellectual property.

UserIdentity is hostName\YourSignInName. hostName is also machineName(server)

Neeless to say you must run your app as administrator if you are in Vista.

On a side note, could you please refrain from posting code with line numbers. It is an additional effort to get rid of them. I had to copy it to my VS2008 to figure out some details. It is not a reprimand:) The line numbers are not needed.

It is a useful chunk of code for me too. I archived it with appreciation. Thanks.