Accessing Text File in Non-Admin mode - c#

I have a text file in Program Files. I cannot write it from a C# application while running in non-admin mode.
I am using this snippet
TextReader read = new StreamReader("C:\Program Files\......\link");
It is throwing an error that the access to the file is denied, however I can read it!
Thanks

Access to a file can be different for reading and writing. As a non-admin, it's normal to be able to read files in Program Files, but not be able to write them.
If the file is a setting for the current user, you should put it in a folder under the AppData folder. You can find the AppData folder's location by calling Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
If the file is a setting for all users on the computer, use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for a list of other possible special folder locations.

Non-admin users don't have write access to files in C:\Program Files by default. If you want to write to a file that is accessible to all users, you should create it in C:\ProgramData.

In .net there is a class File
through which you can use the method
File.read(file path)
this return a string
so you can easily manage it
it also works in a non admin mode

Related

How to save the XML files in Computer?

In my project my app receives an XML from another program. I want to save this XML file in the folder in my PC. But if there is another XML file that will come, the XML will be added in the folder not overwrite the existing XML. How can I achieve it?
I am using this code right now but I don't know why I got an access denied error.
System.IO.File.WriteAllText(#"C:\XML", abc);
First of all, the error that you receive is because of Windows UAC which denies users without Administrator privileges to write on the C:\ disk. Create a folder in which you have access, for example "c:\temp\" and write your files in there.
Also, you specify a file and not folder.
And if you don't want to overwrite the file, just make sure to generate a unique filename (a guid perhaps).

'Access denied' to an xml file in windows application

In my .net windows application am using a xml file .
But after installing the application by creating setup, while I am running that 'Access Denied' to that xml file message is shown.
I am using System.IO.File methods for doing file operations.
If any body knows the solution pls share.
Write access to program directory has been more and more restricted (starting with XP/Vista) since that is a secruity risk!
I would suggest to have the "base version" of that file readonly in your program directory...
Upon start of your app you check whether it is present in ApplicationData or LocalApplicationData or CommonApplicationData - (to get the real path at runtime use Environment.GetFolderPath). If it is not there then copy it there - in those locations your application has write access by default with no need for Administrator rights.
Program Files folder has limited access and can be modified by Administrator account. I would suggest you to save your xml file in *C:\Users\YourPCName\AppData\Local\YourAppFolder* .
This way you will be able to access and modify the file
I had this problem once so I used Isolated Storage and that fixed my problem. It may or may not work for you depending on the nature of your situation, but here's a quick tutorial on how to use it:
http://www.codeproject.com/KB/dotnet/IsolatedStorage.aspx

How to add text file in my project so that it is not visible to user..?

Sir,
I am developing an application in which i am to save some information in text file. During execution, I wish to read/write data from/to the file(.txt file). I dont want the file to be visible to user. Or if it is visible then it must be in programfile folder of the system and user could not delete the data of the file. What should i do? I tried to add the file in Resource file but could not read and write the file. PLease help me to read/write OR provide some other way to implement the same(described above).
Thanks in advance....
Don't use ProgramFiles (installation) folder:
1. File will be visible for user
2. If user won't have admin rights your app will fail on modifying the file.
Windows System folder is also question of rights. I'd advice to use system registry or appdata folder: you can get it Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData). You'll be able to write to that location without admin rights. You should create some special subfolder there (not needed, but would be convinient).
Or in registry case:
writing to registry:
string myEncrStringToSave;
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Your_Cpecial_Key");
key.SetValue("Software\\Your_Cpecial_Key", myEncrStringToSave);
key.Close();
reading from registry:
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Software\\Your_Cpecial_Key");
object value = key.GetValue("Software\\Your_Cpecial_Key");
if (value!=null)
string myEncryptedString = value.ToString();
key.Close();
Your_Cpecial_Key here is some identifier (like in hashtable) that allows you to get access to your data.
you could use IsolatedStorage, it's not fully hidden but at least not easily discoverable as well.
Put the file in the Windows System folder and use it from there.
You can access it without giving full path by using Environment.SpecialFolder.System.
This way user will have to work hard in order to find the file, but in case he does find you can't really prevent him from changing it as it's the same user running your program.
I am not sure if this might help you.
You may make the text file as an Embedded resource and get the same via
Assembly assm = Assembly.GetExecutingAssembly();
StreamReader aread = new StreamReader(assm.GetManifestResourceStream("Namespace.TextFile1.txt"));
So this is not accessible to the user nor available in the deployed location

locate the path of other drive's folder in server

How to locate the path of other drive's folder in server?
For ex:
The application is there in 'C:\SomeFolder\', now I want to export a file to the drive 'D:\AnotherFolder\' on the server. How can I achieve this?
First of all you need to have permissions to access the folder. The user that needs to be able to access the folder is the same user as is running IIS. Usualy it is a built in account IIS_* that is running the instance.
As long as this user has access to the folder, you can just use File Read / Write as you normally do.
Read this article about C# File Handling.
Example - Search for files on another drive
var files = Directory.GetFiles("D:\\", "*.txt");
This will give you an array of files with the extension .txt found on D:\.
Also read this: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
If you mean to get a list of all drives known on a system you can use Environment.GetLogicalDrives.

accessing network path and copy file to the system = c#

i want to create a small c# application. there is a text box and a button in form. if anyone enter a network path for a file and press the button, then the application must copy that file to a folder within that system. How can i do this.?
How to access a network path and how can i copy the file in that path to the system??
You can use the OpenFileDilog class in .net to browse through the files.
Also you can visit these links for Copy and other functionalities.
http://msdn.microsoft.com/en-us/library/cc148994.aspx
http://msdn.microsoft.com/en-us/library/system.io.file.copy(VS.71).aspx
Net work paths are accessed by there full UNC ie \Server\Share\drive\file. As long as you have credentials to access them. You can use system.io.file.copy to move the files.

Categories

Resources