XML Read throws "device not ready" - c#

I'm trying to read a xml file but everytime the code arrives at the load()-function it throws the exeption "the device is not ready". This is the code:
const string filepath = #"E:\xml\somefile.xml";
XmlDocument fileDoc = new XmlDocument();
fileDoc.Load(filepath);
The drive "E:\" is a physical HDD where the webapplication is saved in a folder called "app". So the folder xml is completely independent of the web apllication and iis. I also tried to locate the xml in the application folder but the same error occured.
Has someone an idea what I am missing?

Check if the file "E:\xml\somefile.xml" is not locked in you process when you try to read the xml. Use the right usinging of call Dispose() on the objects which write the file.

Related

C# Winforms cannot create file "the media is write protected"

I have simple code
using (FileStream fs = File.Create(#"newfile.txt"))
{
}
And if I give the path "E:\newfile.txt" I get the error System.IO.IOException: 'The media is write protected.'
If I give the path "C:\newfile.txt" I get no error but no file creation, even if I give the path "C:\Users\Me\Documents\newfile.txt" I get no error and no file creation.
Are these errors related, I have written to USB devices no problem before, and my C drive should not be restricted and surely the Users folder isnt restricted at all.
What am I missing?
Perhaps this might work:
string path = #"C:\Users\Me\Documents\newfile.txt"
File.AppendAllText(path,new[] {"Your text goes here"});
This will create the file if it does not exist or write to the existing one.
See Also:
Create a .txt file if doesn't exist, and if it does append a new line

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)

Save method of XDocument: into which directory will it be saved?

I am using the lines below in the web service to see the XML which we send to another party:
XDocument doc;
doc.Save("san.xml");
In which directory can I find the san.xml file?
When saving a file in c#, if no directory is specified, the application writes to the running directory, which is normally where the application is.

Windows Service cannot create a text file

Ok so this is my code for the OnStart method
File.CreateText("test.txt");
StreamWriter write = File.AppendText("test.txt");
write.WriteLine("Hello world, the service has started");
write.Flush();
write.Close();
I am successfully able to install the service. However when i start i get the message that the service started and then stopped. When i check the Event Viewer it gives me this
Service cannot be started. System.IO.IOException: The process cannot access the file 'C:\Windows\system32\test.txt' because it is being used by another process.
Ok what's going on here. I don't think its a permission problem as the ProcessInstaller is set to LocalSystem.
You do not need to use the first File.CreateText statement. This creates a stream writer on the file which is not closed.
Your File.AppendText tries to create a new StreamWriter on the same file and hence you get the File in use error.
Also, as MSDN says your file will be created if it does not exist.
If the file specified by path does not exist, it is created. If the file does exist, write operations to the StreamWriter append text to the file
You can use like this
string path = #"path\test.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello world, the service has started");
}
}
I think one line code is more then enough.
File.AppendAllText(#"path\test.txt", "Hello world, the service has started");
Appends the specified string to the file, creating the file if it does not already exist.
you should try with the full path for the file, the windows service run in the C:\Windows\System32 folder
string fileName="E:\\Service\\file.txt"
File.Create(file);

How to create a Text file and save it to a shared-directory?

I'm having an issue here, I developed an application in C# which creates a text file. This text file is saved in the X:\Public\3rd\ASN\, the problem is that in development the files are created and saved with no issues but once I move the application into our Web Server the appplication fails and it throws out this error "Could not find a part of the path X:\Public\3rd\ASN\1175_0001.txt".
This is the code I'm using to saved the file in the directory:
w = File.CreateText("X:\Public\Public\3rd\ASN\1175ASN_0001.txt");
Keep in mind that this directory is another server.
Any help will be really appreciate it.
your X drive is a mapped network drive. You need to use the network url eg \\server\directory\Public\3rd\ASN\1175_0001.txt

Categories

Resources