-
September 17th, 2009, 11:22 AM
#1
[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
-
September 17th, 2009, 02:44 PM
#2
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
-
September 17th, 2009, 04:28 PM
#3
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 );
-
September 21st, 2009, 02:29 PM
#4
Re: Store information within program
Originally Posted by Arjay
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.
Originally Posted by Shuja Ali
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
-
September 21st, 2009, 03:31 PM
#5
Re: Store information within program
Originally Posted by vandel212
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.
-
September 21st, 2009, 04:06 PM
#6
Re: Store information within program
Originally Posted by vandel212
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
-
September 22nd, 2009, 11:51 AM
#7
Re: Store information within program
Originally Posted by eclipsed4utoo
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.
Originally Posted by Arjay
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
-
September 22nd, 2009, 12:02 PM
#8
Re: Store information within program
Originally Posted by vandel212
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
-
September 22nd, 2009, 12:37 PM
#9
Re: Store information within program
Originally Posted by eclipsed4utoo
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
-
September 22nd, 2009, 01:08 PM
#10
Re: Store information within program
Originally Posted by vandel212
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
-
September 22nd, 2009, 02:05 PM
#11
Re: Store information within program
Originally Posted by eclipsed4utoo
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.
-
September 22nd, 2009, 02:38 PM
#12
Re: Store information within program
Originally Posted by Arjay
Hey just like in post #3.
Yes....It seemed like he missed that post.
===============================
My Blog
-
September 22nd, 2009, 11:56 PM
#13
Re: Store information within program
Originally Posted by vandel212
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.
-
September 23rd, 2009, 10:47 AM
#14
Re: Store information within program
Originally Posted by eclipsed4utoo
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
-
September 23rd, 2009, 04:41 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|