|
If using local file storage, I normally use the system defined folders as there shouldn't be any permission problems reading/writing from/to these.
string userPath =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string allUsersPath =
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
[Edit] There are some problems if a different user needs to overwrite an existing file in CommonApplicationData. See solution here[^]. [/Edit]
|
|
|
|
|
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.
|
|
|
|
|
Not tried it under Weven, but my test dev machine with weven is fired up so will test it now
|
|
|
|
|
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]
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
thanks again. And a nice cliff hanger. 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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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)
{
Console.WriteLine(ex);
}
Console.ReadKey();
}
private static void CreateDirectoryWithPermissions(string path)
{
if (!Directory.Exists(path))
{
bool modified = false;
DirectoryInfo directoryInfo = directoryInfo = Directory.CreateDirectory(path);
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
AccessRule rule = new FileSystemAccessRule(
"Users",
FileSystemRights.Write |
FileSystemRights.ReadAndExecute |
FileSystemRights.Modify,
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
directoryInfo.SetAccessControl(directorySecurity);
}
}
}
}
|
|
|
|
|
|
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.
|
|
|
|
|
Didn't know that
|
|
|
|
|
|
Tip posted here[^] with an improved reusable version of the solution I posted earlier.
|
|
|
|
|
Hi,
Can anyone please tell me if you know about any good "version control software"? Working in a live project of C#. Since its improving 'everyday', need to save every version of projects with description, which have been delivered to the customer already.
Any idea?
|
|
|
|
|
Git[^] seems very popular.
I haven't used it though, so I can't tell how good it is. Kristian Sixhoej
"You can always become better." - Tiger Woods
|
|
|
|
|
I am using VisualSVNServer. with following plugins as clients
For visual studio, AnkhSVN works great.
For Files and Forders, TortoiseSVN works great.
|
|
|
|
|
Hi Som,
Thanks. i installed this software with plugins, but not finding any direct way to use it, bit confusing...
|
|
|
|
|
May be This[^] could be a right place to start
|
|
|
|
|
Hi Som,
Thanks a lot, This guide seems to be really helpful. Now I am sure i'll be able to manage it.
Regards. /Khan.
|
|
|
|
|
|
SourceGear Vault, www.sourcegear.com[^], is free for a single user license.Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
Help humanity, join the CodeProject grid computing team here
|
|
|
|
|