I have made a windows service in C#, what this service does is read a text file and convert it to json, but what I want to know is how can I delete that txt file once converted to json, it doesn't allow me to delete it because I get this log, "The process cannot access the file 'C:\API\inbox\13203530500100000060800712202231100.TXT' because it is being used by another process."
I am using the following to get the txt of the folder:
string[] files = Directory.GetFiles(rutaRaiz, "*.txt");
thanks for the support,
use the File.Delete(item) to delete the txt but it gave me error:
, "The process cannot access the file 'C:\API\inbox\13203530500100000060800712202231100.TXT' because it is being used by another process."
Related
I have a byte array and need to save it to a file.
I have tried the below code:
File.WriteAllBytes("form.txt", byteArray);
string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
I have referred https://stackoverflow.com/a/19455387/15265496 for the implementation.
I am looking for the file in Android emulator. Where will the file get saved from first line?
Should I create a form.txt in the application local folder?
Is there any alternative way to do the same?
You can find the appropriate folder using Environment.SpecialFolder.LocalApplicationData
string fileExactLocation = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "form.txt");
You can find more information at https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows
You cannot access the file system of an Android emulator instance via explorer or finder.
As per the Microsoft docs, if you want to store a file in Android, you can always use the application's files folder which is accessible through:
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
A file saved to that folder will go to
/data/user/0/com.companyname/files
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
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).
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
For my system, i first need to read CSV file and then i need to connect to the database in SQL server 2008 according to the data in .ini file.
I use System.IO.FileStream() to read file. There is no problem to open and read data.
But, when i read csv file and then connect to the database , i cant access to the .ini file
because System.IO.FileStream() function take the path of ini file like as the location of csv file.
So, when i read csv file from Desktop , System.IO.FileStream() function search the ini file in Desktop and when i read from My Document, it search in My Document.
Thus, i want to know how to control this.
My function to read ini file : System.IO.FileStream("fileNameOnly", System.IO.FileMode.Open, System.IO.FileAccess.Read);
You can set your OpenFileDialog.RestoreDirectory to True to have the dialog reset its location on each use.