CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 34
  1. #1
    Join Date
    Feb 2009
    Posts
    112

    [RESOLVED] Store information within program

    I'm makeing a program that users will be able to have run on start up. In this program I need to create temporary files in their My Pictures folder. Unfortunatly sometimes this folder does not exist or user's profile has changed and the Evironment.user is no longer accurate. So what I would like to do is have the program check to see if the My Pictures folder exsists. If it does... wonderful. If it doesn't I have the user choose a directory where the temporary files can be created. My question is, is there a way that I can take the users chosen directory and be able to use it again next time the program starts up? I would like to do this without creating another file in which I would store the information (i.e. create a text file with the directory path in it). The reason is because the people that use this program don't have privlages to write anywhere. So I would like have the chosen directory be stored in the program if it is possible.

    Thanks in advance.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Store information within program

    You can use settings in C# to store information pertaining to the application.
    Take a look at http://msdn.microsoft.com/en-us/libr...69(VS.80).aspx

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Store information within program

    Why not write the temporary files in the proper location rather in the My pictures folder?

    See Environment.GetFolderPath( SpecialFolder.ApplicationData );

  4. #4
    Join Date
    Feb 2009
    Posts
    112

    Re: Store information within program

    Quote Originally Posted by Arjay View Post
    Why not write the temporary files in the proper location rather in the My pictures folder?

    See Environment.GetFolderPath( SpecialFolder.ApplicationData );

    Unfortunatly the users of this program do not have sufficient privlages to save files anywhere other than their documents. The reason I need to add this capability to my program is because the users name may have changed (ie. someone got married) so their log on info chaged but not how it is accessed in their my documents. I have no idea why my company would allow this but I don't care, I get paid at the end of the week regardless. Also It allows me to learn new things about programing. Hopefully that explained my needs for this a little better.

    Quote Originally Posted by Shuja Ali View Post
    You can use settings in C# to store information pertaining to the application.
    Take a look at http://msdn.microsoft.com/en-us/libr...69(VS.80).aspx
    I tried doing it and what I would like to do is have a string in the settings that points to "C:\\Documents and Settings\\Environment.UserName\\My Documents\\My Pictures", but it doesn't work because it doesn't make "Environment.UserName" into the users user name. Is there any way to make this work.


    Thanks for the help guys.
    Last edited by vandel212; September 21st, 2009 at 02:53 PM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Store information within program

    Quote Originally Posted by vandel212 View Post
    Unfortunatly the users of this program do not have sufficient privlages to save files anywhere other than their documents. The reason I need to add this capability to my program is because the users name may have changed (ie. someone got married) so their log on info chaged but not how it is accessed in their my documents. I have no idea why my company would allow this but I don't care, I get paid at the end of the week regardless. Also It allows me to learn new things about programing. Hopefully that explained my needs for this a little better.
    As a user, I would find it irritating to discover temporary files created by some application in the My Pictures folder. It seems surprising that a company would lock down the ApplicationData folder and break the functionality of many apps.

    Check out http://msdn.microsoft.com/en-us/libr...istauac_topic6

    and look at the section titled Per-User Application Settings Locations.

  6. #6
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Store information within program

    Quote Originally Posted by vandel212 View Post
    I tried doing it and what I would like to do is have a string in the settings that points to "C:\\Documents and Settings\\Environment.UserName\\My Documents\\My Pictures", but it doesn't work because it doesn't make "Environment.UserName" into the users user name. Is there any way to make this work.


    Thanks for the help guys.
    Code:
    string path = string.Format(@"C:\Documents and Settings\{0}\My Documents\My Pictures", Environment.UserName);
    ===============================
    My Blog

  7. #7
    Join Date
    Feb 2009
    Posts
    112

    Re: Store information within program

    Quote Originally Posted by eclipsed4utoo View Post
    Code:
    string path = string.Format(@"C:\Documents and Settings\{0}\My Documents\My Pictures", Environment.UserName);
    I'm sorry I don't understand, is this supposed to do this a design time or am I supposed to do this a run time, because putting it in the settings at design time didn't work.

    Quote Originally Posted by Arjay View Post
    As a user, I would find it irritating to discover temporary files created by some application in the My Pictures folder. It seems surprising that a company would lock down the ApplicationData folder and break the functionality of many apps.
    Application Data would be a better place to save it, thanks for the tip. They didn't lock down anything in the users documents, so Application Data would still be a valid folder. Regardless I would still need to have settings to account for changed user names.
    Last edited by vandel212; September 22nd, 2009 at 11:57 AM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  8. #8
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Store information within program

    Quote Originally Posted by vandel212 View Post
    I'm sorry I don't understand, is this supposed to do this a design time or am I supposed to do this a run time, because putting it in the settings at design time didn't work.
    You do that at design time in code. When executed, it will give you the path with the current username.

    Since you are going to using the Application Data folder, this isn't needed anymore.
    ===============================
    My Blog

  9. #9
    Join Date
    Feb 2009
    Posts
    112

    Re: Store information within program

    Quote Originally Posted by eclipsed4utoo View Post
    You do that at design time in code. When executed, it will give you the path with the current username.

    Since you are going to using the Application Data folder, this isn't needed anymore.
    Why wouldn't it be needed the Application Data? It would still have to go to the user's specific folder in documents and settings.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  10. #10
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Store information within program

    Quote Originally Posted by vandel212 View Post
    Why wouldn't it be needed the Application Data? It would still have to go to the user's specific folder in documents and settings.
    if you use the built-in methods, you don't have to worry about it.

    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    This will return "C:\Documents and Settings\[username]\Application Data". So you don't need to worry about the user name. .Net will take care of that for you.
    ===============================
    My Blog

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Store information within program

    Quote Originally Posted by eclipsed4utoo View Post
    if you use the built-in methods, you don't have to worry about it.

    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    This will return "C:\Documents and Settings\[username]\Application Data". So you don't need to worry about the user name. .Net will take care of that for you.
    Hey just like in post #3.

  12. #12
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: Store information within program

    Quote Originally Posted by Arjay View Post
    Hey just like in post #3.
    Yes....It seemed like he missed that post.
    ===============================
    My Blog

  13. #13
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: Store information within program

    Quote Originally Posted by vandel212 View Post
    I. My question is, is there a way that I can take the users chosen directory and be able to use it again next time the program starts up? I would like to do this without creating another file in which I would store the information (i.e. create a text file with the directory path in it). .
    try writing the folder path in registry or config file corresponding to the current user.. each time when you start up try reading this file.

  14. #14
    Join Date
    Feb 2009
    Posts
    112

    Re: Store information within program

    Quote Originally Posted by eclipsed4utoo View Post
    if you use the built-in methods, you don't have to worry about it.

    Code:
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    This will return "C:\Documents and Settings\[username]\Application Data". So you don't need to worry about the user name. .Net will take care of that for you.
    I understand that .NET will get the user name for me but that is the problem. I've already run into this problem with the program I writing now. Let me do an example and you can tell me what I'm missing because I'm sure I'm missing something. I try to make it go to Environment.UserName, lets say their user name is jane.doe. Jane Doe gets married and changes her last name. Now her name is Jane Smith. Her username on the computer is now jane.smith, but the documents and settings folder for her is still under jane.doe. This is the problem because if I use Environment.UserName it will go to: "C:\Documents and Settings\jane.smith" which is not a valid folder. I know it is really strange that the user's documents and settings folder didn't change but I have no control over that, unfortunatly I have no choice but to work with it.

    So is the way you mentioned before going to work?


    Also this is what I tried for the settings stuff and I can't get it to work.

    Code:
    namespace Red_Lining_Application.Properties {
        
        
        [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
        internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
            
            private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
            
            public static Settings Default {
                get {
                    return defaultInstance;
                }
            }
            string path = string.Format(@"C:\Documents and Settings\{0}\My Documents\My Pictures", Environment.UserName); // I get an error trying to use Environment.Username.
            [global::System.Configuration.UserScopedSettingAttribute()]
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.Configuration.DefaultSettingValueAttribute(path)]
            public string UserPath {
                get {
                    return ((string)(this["UserPath"]));
                }
                set {
                    this["UserPath"] = value;
                }
            }
        }
    }
    even if I put in "using System;" it still throws an error because it is not a constant vaule. I don't think I completly understand what you meant by you use it in design time in the code. I went to Settings.designer.cs. thats where I'm supposed to go right?
    Last edited by vandel212; September 23rd, 2009 at 11:58 AM.
    - It's a long way to the top if you want to rock n' roll - AC/DC

    Check out my band and support the music:
    www.blueruinmusic.com

  15. #15
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Store information within program

    Code:
    namespace Red_Lining_Application.Properties {
        [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
        internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
     
            private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
     
            public static Settings Default {
                get {
                    return defaultInstance;
                }
            }
            string path = string.Format(@"C:\Documents and Settings\{0}\My Documents\My Pictures", Environment.UserName); // I get an error trying to use Environment.Username.
            [global::System.Configuration.UserScopedSettingAttribute()]
            [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
            [global::System.Configuration.DefaultSettingValueAttribute(path)]
            public string UserPath {
                get {
                    return ((string)(this["UserPath"]));
                }
                set {
                    this["UserPath"] = value;
                }
            }
        }
    }
    Dont do this in the Properties itself ! Do this where your code wants to read the UserPath
    Simple instead of accessing the Properties
    Code:
    string userpath = Properties.Settings.Default.UserPath;
    // do
    string userpath =  string.Format(@"C:\Documents and Settings\{0}\My Documents\My Pictures", Environment.UserName);
    No need to have that code in the Properties. !!
    Last edited by JonnyPoet; September 24th, 2009 at 08:51 AM.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured