System.IO.File.WriteAllLines exception
-
I have compiled successfully a C# console project. And when I run this app is stopping at the line:
System.IO.File.WriteAllLines(@destFile, elem);
where
@destFile
isstring destFile
and
elem
isstring[] elem = new string[...]
The error is:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'e:\' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllLines(String path, String[] contents)Of course, I have ran this app as administrator rights, with the same results. What should I understand from this error ? And how can I solve it ? My C# knowledge is thin ...
-
I have compiled successfully a C# console project. And when I run this app is stopping at the line:
System.IO.File.WriteAllLines(@destFile, elem);
where
@destFile
isstring destFile
and
elem
isstring[] elem = new string[...]
The error is:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'e:\' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllLines(String path, String[] contents)Of course, I have ran this app as administrator rights, with the same results. What should I understand from this error ? And how can I solve it ? My C# knowledge is thin ...
-
I have compiled successfully a C# console project. And when I run this app is stopping at the line:
System.IO.File.WriteAllLines(@destFile, elem);
where
@destFile
isstring destFile
and
elem
isstring[] elem = new string[...]
The error is:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'e:\' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllLines(String path, String[] contents)Of course, I have ran this app as administrator rights, with the same results. What should I understand from this error ? And how can I solve it ? My C# knowledge is thin ...
What do you expect
@destFile
to be? You do not need an "@" in front of a variable name unless you are trying to use a keyword as a variable name - and if you are doing that, then you need to rethink your coding style. To call somethingpublic
orstring
would require the "@" prefix, because they are both specific words that the compiler understands - butdestFile
is not, and does not need it.int @public = 666;
Console.WriteLine(@public);
...
string destFile = "...";
Console.WriteLine(destFile);And if you don't assign a value to
destFile
before you use it as an output location, you will get an error - it needs to contain a valid filename, in a folder that is not restricted for security reasons:string destFile = "D:\Test Data\byList.csv";
File.WriteAllLines(destFile, elem);So use the debugger to examine exactly what you have in
destFile
before you try to write to the file and check it's a fully valid filename! If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
What do you expect
@destFile
to be? You do not need an "@" in front of a variable name unless you are trying to use a keyword as a variable name - and if you are doing that, then you need to rethink your coding style. To call somethingpublic
orstring
would require the "@" prefix, because they are both specific words that the compiler understands - butdestFile
is not, and does not need it.int @public = 666;
Console.WriteLine(@public);
...
string destFile = "...";
Console.WriteLine(destFile);And if you don't assign a value to
destFile
before you use it as an output location, you will get an error - it needs to contain a valid filename, in a folder that is not restricted for security reasons:string destFile = "D:\Test Data\byList.csv";
File.WriteAllLines(destFile, elem);So use the debugger to examine exactly what you have in
destFile
before you try to write to the file and check it's a fully valid filename! If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need. Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!
-
I have compiled successfully a C# console project. And when I run this app is stopping at the line:
System.IO.File.WriteAllLines(@destFile, elem);
where
@destFile
isstring destFile
and
elem
isstring[] elem = new string[...]
The error is:
System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'e:\' is denied.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllLines(String path, String[] contents)Of course, I have ran this app as administrator rights, with the same results. What should I understand from this error ? And how can I solve it ? My C# knowledge is thin ...
I don't know what happened to the reply you sent to me, so I reproduce it below:
Quote:
Believe me I have tried a lot of combination (e.g. "D:\\tempx\\" or "d:\\tempx"), the same result.
It seems to me you are confused between directory names and file names. You cannot use Write of any sort to write into a directory, you must use a proper file name. So something like "E:\\mytextfile.txt", "D:\\tempx\\mytextfile.txt", is what is needed.
-
I don't know what happened to the reply you sent to me, so I reproduce it below:
Quote:
Believe me I have tried a lot of combination (e.g. "D:\\tempx\\" or "d:\\tempx"), the same result.
It seems to me you are confused between directory names and file names. You cannot use Write of any sort to write into a directory, you must use a proper file name. So something like "E:\\mytextfile.txt", "D:\\tempx\\mytextfile.txt", is what is needed.