Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.IO.File.WriteAllLines exception

System.IO.File.WriteAllLines exception

Scheduled Pinned Locked Moved C#
questioncsharpsecurityhelplearning
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _Flaviu
    wrote on last edited by
    #1

    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 is

    string destFile

    and elem is

    string[] 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 ...

    L OriginalGriffO 3 Replies Last reply
    0
    • _ _Flaviu

      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 is

      string destFile

      and elem is

      string[] 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 ...

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You cannot write a file to "E:\"; that is not a valid file name.

      _ 1 Reply Last reply
      0
      • _ _Flaviu

        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 is

        string destFile

        and elem is

        string[] 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 ...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 something public or string would require the "@" prefix, because they are both specific words that the compiler understands - but destFile 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 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

        _ 1 Reply Last reply
        0
        • L Lost User

          You cannot write a file to "E:\"; that is not a valid file name.

          _ Offline
          _ Offline
          _Flaviu
          wrote on last edited by
          #4

          Believe me I have tried a lot of combination (e.g. "D:\\tempx\\" or "d:\\tempx"), the same result.

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            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 something public or string would require the "@" prefix, because they are both specific words that the compiler understands - but destFile 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!

            _ Offline
            _ Offline
            _Flaviu
            wrote on last edited by
            #5

            Thank you for detailed explanation, the code is not written by me, so I try to adapt it as much as I can.

            1 Reply Last reply
            0
            • _ _Flaviu

              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 is

              string destFile

              and elem is

              string[] 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 ...

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              _ 1 Reply Last reply
              0
              • L Lost User

                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.

                _ Offline
                _ Offline
                _Flaviu
                wrote on last edited by
                #7

                Yes, I figure out that and now I am trying to move on.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups