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. stores files for my project

stores files for my project

Scheduled Pinned Locked Moved C#
question
19 Posts 5 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.
  • L Luc Pattyn

    Hi Dave, FYI: I haven't used Win7 myself yet, however Christian[^] reported some problems with appdata. Any comment on the matter? :)

    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
    All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


    D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #6

    This works for me... Test Code:

    using System;
    using System.IO;

    namespace ConsoleApplication
    {
    class Program
    {
    public static readonly string UserPath =
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    public static readonly string AllUsersPath =
    Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

        static void Main(string\[\] args)
        {
            try
            {
                string fileName = "Test.txt";
                string textToSave = "AbcDefGhi";
    
                string userFilePath = Path.Combine(UserPath, fileName);
                string allUsersFilePath = Path.Combine(AllUsersPath, fileName);
    
                using (TextWriter userTextWriter = new StreamWriter(userFilePath),
                allUsersTextWriter = new StreamWriter(allUsersFilePath))
                {
                    Console.WriteLine("Writing {0} to {1}", textToSave, userFilePath);
                    userTextWriter.WriteLine(textToSave);
                    Console.WriteLine("Writing {0} to {1}", textToSave, allUsersFilePath);
                    allUsersTextWriter.WriteLine(textToSave);
                }
    
                using (TextReader userTextReader = new StreamReader(userFilePath),
                allUsersTextReader = new StreamReader(allUsersFilePath))
                {
                    Console.WriteLine("Reading from {0}", userFilePath);
                    Console.WriteLine(userTextReader.ReadLine());
                    Console.WriteLine("Reading from {0}", allUsersFilePath);
                    Console.WriteLine(allUsersTextReader.ReadLine());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.ReadKey();
        }
    }
    

    }

    [Edit] Added exception checking [/Edit] [Edit2] Removed inner exception log as per Luc's post below [/Edit2]

    Dave
    Tip: Passing values between objects using events (C#)
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
    Why are you using VB6? Do you hate yourself? (Christian Graus)

    L 2 Replies Last reply
    0
    • D DaveyM69

      This works for me... Test Code:

      using System;
      using System.IO;

      namespace ConsoleApplication
      {
      class Program
      {
      public static readonly string UserPath =
      Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
      public static readonly string AllUsersPath =
      Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

          static void Main(string\[\] args)
          {
              try
              {
                  string fileName = "Test.txt";
                  string textToSave = "AbcDefGhi";
      
                  string userFilePath = Path.Combine(UserPath, fileName);
                  string allUsersFilePath = Path.Combine(AllUsersPath, fileName);
      
                  using (TextWriter userTextWriter = new StreamWriter(userFilePath),
                  allUsersTextWriter = new StreamWriter(allUsersFilePath))
                  {
                      Console.WriteLine("Writing {0} to {1}", textToSave, userFilePath);
                      userTextWriter.WriteLine(textToSave);
                      Console.WriteLine("Writing {0} to {1}", textToSave, allUsersFilePath);
                      allUsersTextWriter.WriteLine(textToSave);
                  }
      
                  using (TextReader userTextReader = new StreamReader(userFilePath),
                  allUsersTextReader = new StreamReader(allUsersFilePath))
                  {
                      Console.WriteLine("Reading from {0}", userFilePath);
                      Console.WriteLine(userTextReader.ReadLine());
                      Console.WriteLine("Reading from {0}", allUsersFilePath);
                      Console.WriteLine(allUsersTextReader.ReadLine());
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex);
              }
              Console.ReadKey();
          }
      }
      

      }

      [Edit] Added exception checking [/Edit] [Edit2] Removed inner exception log as per Luc's post below [/Edit2]

      Dave
      Tip: Passing values between objects using events (C#)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
      Why are you using VB6? Do you hate yourself? (Christian Graus)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #7

      Thanks. assuming you are logged in as a regular user, not a sysadmin, and UAC is enabled and working normally, that means Win7 behaves like Vista in this, and CG somehow ran out of luck again!? :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


      D 2 Replies Last reply
      0
      • L Luc Pattyn

        Thanks. assuming you are logged in as a regular user, not a sysadmin, and UAC is enabled and working normally, that means Win7 behaves like Vista in this, and CG somehow ran out of luck again!? :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #8

        Yeah, just a regular default Win7 install with no tweaking. The account is under the 'Administrators' group as is the default for the first installed user rather than a limited account, but scratching a [insert body part here] still requires UAC elevation so no special permissions have been requested or granted to the account. I think CG has just been 'Graused' again. :laugh:

        Dave
        Tip: Passing values between objects using events (C#)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • L Luc Pattyn

          Thanks. assuming you are logged in as a regular user, not a sysadmin, and UAC is enabled and working normally, that means Win7 behaves like Vista in this, and CG somehow ran out of luck again!? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #9

          Interesting... I just created a 'Standard User' account and tested the test code .exe under that account and it failed! Even the exception wasn't caught! Just a Win7 dialog, un-collapsing the details panel shows

          Problem Event Name: CLR20r3
          Problem Signature 01: consoleapplication.exe
          Problem Signature 02: 1.0.0.0
          Problem Signature 03: 4b89d18d
          Problem Signature 04: mscorlib
          Problem Signature 05: 2.0.0.0
          Problem Signature 06: 4a275af7
          Problem Signature 07: 344b
          Problem Signature 08: 15a
          Problem Signature 09: System.UnauthorizedAccess
          OS Version: 6.1.7600.2.0.0.256.1
          Locale ID: 2057

          I'll invesigate this more tomorrow :confused:

          Dave
          Tip: Passing values between objects using events (C#)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          L 1 Reply Last reply
          0
          • D DaveyM69

            Interesting... I just created a 'Standard User' account and tested the test code .exe under that account and it failed! Even the exception wasn't caught! Just a Win7 dialog, un-collapsing the details panel shows

            Problem Event Name: CLR20r3
            Problem Signature 01: consoleapplication.exe
            Problem Signature 02: 1.0.0.0
            Problem Signature 03: 4b89d18d
            Problem Signature 04: mscorlib
            Problem Signature 05: 2.0.0.0
            Problem Signature 06: 4a275af7
            Problem Signature 07: 344b
            Problem Signature 08: 15a
            Problem Signature 09: System.UnauthorizedAccess
            OS Version: 6.1.7600.2.0.0.256.1
            Locale ID: 2057

            I'll invesigate this more tomorrow :confused:

            Dave
            Tip: Passing values between objects using events (C#)
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Why are you using VB6? Do you hate yourself? (Christian Graus)

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #10

            thanks again. And a nice cliff hanger. :laugh:

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


            D 2 Replies Last reply
            0
            • D DaveyM69

              This works for me... Test Code:

              using System;
              using System.IO;

              namespace ConsoleApplication
              {
              class Program
              {
              public static readonly string UserPath =
              Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
              public static readonly string AllUsersPath =
              Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                  static void Main(string\[\] args)
                  {
                      try
                      {
                          string fileName = "Test.txt";
                          string textToSave = "AbcDefGhi";
              
                          string userFilePath = Path.Combine(UserPath, fileName);
                          string allUsersFilePath = Path.Combine(AllUsersPath, fileName);
              
                          using (TextWriter userTextWriter = new StreamWriter(userFilePath),
                          allUsersTextWriter = new StreamWriter(allUsersFilePath))
                          {
                              Console.WriteLine("Writing {0} to {1}", textToSave, userFilePath);
                              userTextWriter.WriteLine(textToSave);
                              Console.WriteLine("Writing {0} to {1}", textToSave, allUsersFilePath);
                              allUsersTextWriter.WriteLine(textToSave);
                          }
              
                          using (TextReader userTextReader = new StreamReader(userFilePath),
                          allUsersTextReader = new StreamReader(allUsersFilePath))
                          {
                              Console.WriteLine("Reading from {0}", userFilePath);
                              Console.WriteLine(userTextReader.ReadLine());
                              Console.WriteLine("Reading from {0}", allUsersFilePath);
                              Console.WriteLine(allUsersTextReader.ReadLine());
                          }
                      }
                      catch (Exception ex)
                      {
                          Console.WriteLine(ex);
                      }
                      Console.ReadKey();
                  }
              }
              

              }

              [Edit] Added exception checking [/Edit] [Edit2] Removed inner exception log as per Luc's post below [/Edit2]

              Dave
              Tip: Passing values between objects using events (C#)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #11

              if you ask for Exception.ToString() there is no need to remember inner exceptions at all... :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              D 1 Reply Last reply
              0
              • L Luc Pattyn

                thanks again. And a nice cliff hanger. :laugh:

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #12

                Couldn't go to bed with that on my mind! The problem is I left the file in the 'CommonApplicationData' folder (X:\ProgramData\Test.txt) from when I ran under the admin account. The Standard User account could not overwrite it as it was created by a user from the Administrators group. I deleted it using the admin account, switched to the Standard User, it ran perfectly and could overwrite the file it created itself with no issues on multiple runs.

                Dave
                Tip: Passing values between objects using events (C#)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                1 Reply Last reply
                0
                • L Luc Pattyn

                  if you ask for Exception.ToString() there is no need to remember inner exceptions at all... :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                  All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #13

                  :thumbsup: Didn't know that :)

                  Dave
                  Tip: Passing values between objects using events (C#)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Why are you using VB6? Do you hate yourself? (Christian Graus)

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    thanks again. And a nice cliff hanger. :laugh:

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                    All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #14

                    More curious... The file created by the Standard User can't be overwritten by the user from the 'Administrators' group either.

                    System.UnauthorizedAccessException:
                    Access to the path 'X:\ProgramData\Test.txt' is denied.

                    Dave
                    Tip: Passing values between objects using events (C#)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    L 1 Reply Last reply
                    0
                    • D DaveyM69

                      More curious... The file created by the Standard User can't be overwritten by the user from the 'Administrators' group either.

                      System.UnauthorizedAccessException:
                      Access to the path 'X:\ProgramData\Test.txt' is denied.

                      Dave
                      Tip: Passing values between objects using events (C#)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Why are you using VB6? Do you hate yourself? (Christian Graus)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #15

                      yeah, I would expect: 1. AppData to be individual and hence writable and readable for the user account; 2. CommonAppData to be common to everyone, hence readable by everyone, but writable only by someone who can install the app. Haven't seen it documented in any detail however. BTW: if number 2 is what MS intended, CommonAppData does not make much sense, the folder holding the EXE would offer the same characteristics. [EDIT] Well, it does keep settings apart from distributed code, so app settings would have a better chance of surviving an app upgrade[/EDIT] :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                      All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                      D 2 Replies Last reply
                      0
                      • L Luc Pattyn

                        yeah, I would expect: 1. AppData to be individual and hence writable and readable for the user account; 2. CommonAppData to be common to everyone, hence readable by everyone, but writable only by someone who can install the app. Haven't seen it documented in any detail however. BTW: if number 2 is what MS intended, CommonAppData does not make much sense, the folder holding the EXE would offer the same characteristics. [EDIT] Well, it does keep settings apart from distributed code, so app settings would have a better chance of surviving an app upgrade[/EDIT] :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                        All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                        D Offline
                        D Offline
                        DaveyM69
                        wrote on last edited by
                        #16

                        All users get read and execute permissions on folders/files created there - but not write/modify. It can be altered of course by creating a folder for your app and setting permissions at that time. Modified code below works so all users have Write and Modify as well so the store can be truly shared. I'll clean up the code and put it into a reusable class and post as a Tip/Trick tomorrow.

                        using System;
                        using System.IO;
                        using System.Security.AccessControl;

                        namespace ConsoleApplication
                        {
                        class Program
                        {
                        public static readonly string UserPath =
                        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                        public static readonly string AllUsersPath =
                        Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                            static void Main(string\[\] args)
                            {
                                try
                                {
                                    string folderName = "Test";
                                    string fileName = "Test.txt";
                                    string textToSave = "AbcDefGhi";
                        
                                    string userDirectory = Path.Combine(UserPath, folderName);
                                    string allUsersDirectory = Path.Combine(AllUsersPath, folderName);
                        
                                    string userFilePath = Path.Combine(userDirectory, fileName);
                                    string allUsersFilePath = Path.Combine(allUsersDirectory, fileName);
                        
                                    CreateDirectoryWithPermissions(userDirectory);
                                    CreateDirectoryWithPermissions(allUsersDirectory);
                        
                                    using (TextWriter userTextWriter = new StreamWriter(userFilePath),
                                    allUsersTextWriter = new StreamWriter(allUsersFilePath))
                                    {
                                        Console.WriteLine("Writing {0} to {1}", textToSave, userFilePath);
                                        userTextWriter.WriteLine(textToSave);
                                        Console.WriteLine("Writing {0} to {1}", textToSave, allUsersFilePath);
                                        allUsersTextWriter.WriteLine(textToSave);
                                    }
                        
                                    using (TextReader userTextReader = new StreamReader(userFilePath),
                                    allUsersTextReader = new StreamReader(allUsersFilePath))
                                    {
                                        Console.WriteLine("Reading from {0}", userFilePath);
                                        Console.WriteLine(userTextReader.ReadLine());
                                        Console.WriteLine("Reading from {0}", allUsersFilePath);
                                        Console.WriteLine(allUsersTextReader.ReadLine());
                                    }
                                }
                                catch (Exception ex)
                                {
                        
                        1 Reply Last reply
                        0
                        • J jogisarge

                          Hello, i am using VS2008 and SQLServer 2005. my user should be able to store files/pictures in my app. what is the best way to save files like pictures at runtime ? where should i store them ? thanks bye jo

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #17

                          C# Photo Album Viewer[^]


                          I know the language. I've read a book. - _Madmatt

                          1 Reply Last reply
                          0
                          • J jogisarge

                            Hello, i am using VS2008 and SQLServer 2005. my user should be able to store files/pictures in my app. what is the best way to save files like pictures at runtime ? where should i store them ? thanks bye jo

                            D Offline
                            D Offline
                            DaveyM69
                            wrote on last edited by
                            #18

                            Tip posted here[^] with an improved reusable version of the solution I posted earlier.

                            Dave
                            Tip: Passing values between objects using events (C#)
                            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                            Why are you using VB6? Do you hate yourself? (Christian Graus)

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              yeah, I would expect: 1. AppData to be individual and hence writable and readable for the user account; 2. CommonAppData to be common to everyone, hence readable by everyone, but writable only by someone who can install the app. Haven't seen it documented in any detail however. BTW: if number 2 is what MS intended, CommonAppData does not make much sense, the folder holding the EXE would offer the same characteristics. [EDIT] Well, it does keep settings apart from distributed code, so app settings would have a better chance of surviving an app upgrade[/EDIT] :)

                              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                              D Offline
                              D Offline
                              DaveyM69
                              wrote on last edited by
                              #19

                              Tip/Trick here[^].

                              Dave
                              Tip: Passing values between objects using events (C#)
                              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                              Why are you using VB6? Do you hate yourself? (Christian Graus)

                              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