How to save the XML files in Computer? - c#

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).

Related

How to save XML file on IIS 7 server using ASP.NET

So I have a problem in my ASP.NET MVC application, it doesn't want to save the xml file after I publish it. I have a form which I post to a controller using ajax, and then I use that data to create an xml file which i then want to save.
I use the following code to generate my xml file and save it:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rawXml);
StreamWriter path = new StreamWriter(Server.MapPath("/"+ fileName + ".xml"));
xmlDoc.Save(path);
If I run my application in debug It writes the file to ~/MySolution/MyProject/MyFile, no problem.
So when I publish the app to the IIS 7 server on my computer and load the app through localhost/MyApp, I expect it to write the file to C:\inetpub\wwwroot\MyApp\MyFile but it doesn't.
I have enabled permissions to the folder inetpub and all the subsequent folders for NETWORK SERVICE. But the AJAX post keeps returning in Error and the file doesn't appear in the folder, so I assume it's not allowing to write the file to the specified path, or the path is incorrect, ether way I don't know how to check what's gone wrong.
How do I make the published app write the xml file to the C:\inetpub\wwwroot\MyApp\MyFile path?
First of all it's not recommended to write any files in the root folder as writing to root folder cause appdomain to recycle after certain number of writes (default is 15) causing session loss. See more details here.
I would suggest you to add a path of your server to web.config and then fetch it in your code.Use something like below in the appsettings section of web.config
<add key="filePath" value="C:\inetpub\wwwroot\MyApp" />
Regarding the permissions please add Users group to your folder and give that group full permission (read/write).
To further find out which specific user (as there are too many use cases) is used by w3wp you can use process monitor as explained below
Capture Process Monitor log while reproducing issue
Filter on Access Denied
No Access Denied, so filter on w3wp.exe
Look for access to 401-3.htm
Review entries before the 401-3.htm to determine what file was accessed last
Check permissions based on successful QuerySecurityFile operation for last file accessed (in this case was asp.dll)

'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

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.

How do I get the filename of the .msg file when I use specifiedPickupDirectory for my .Net SMTP

I'm trying to keep track of the email created after I "send" it using SmtpClient.Send.
I have it configured to write to a directory by configuring my app.config to use specifiedPickupDirectory.
What I'd like to gain access to is the name of the file that was used, so that I can periodically check and make sure that my mail server has retrieved it and sent it along.
Any suggestions?
Perhaps try initially creating the file in a temporary directory. Make sure that it is the only file in the directory. Use Directory.GetFiles to find the file name and save it to a variable. Then move the file to the real directory.

Accessing Text File in Non-Admin mode

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

Categories

Resources