F# - Weird FileNotFound Exception - f#

I basically just have this:
open System.Net
open System.IO
let reader = new StreamReader("students.txt")
let csv = reader.ReadToEnd()
For some reason this throws a File Not Found Exception. It tells me it could not find the file at "C:\Users\Shane\ownCloud\Home\Assign18\Assign18\bin\Debug\students.txt" even though that is exactly where the file is. This happens even if I put the full file path to students.txt, or if I move it to another location.
Anyone have any idea what is going on?

Turns out I'm just not very used to using Windows (only running it for Visual Studio). So when I named the file "Students.txt", I didn't realize that Windows already had a file extension for that file that it was hiding from me. So the whole time, the file was actually called "Students.txt.txt"

Make sure to run the program as an administrator - new StreamReader(string) does not mention a specific exception when file permissions are violated, and thus may be wrapping it.
Note that File.Open and the aformentioned File.ReadAllText methods mention specific exceptions when encountering access violations, and thus may throw more descriptive (helpful) exceptions.

Related

CreateDirectory in Environment.SpecialFolder.MyDocuments is denied

I have a file stream that I want to store in iOS under a subfolder under the SpecialFolder enum.
The FileStream constructor wants the subfolder structure to exist first and when I try to create it, I get UnauthorizedAccessException.
Suppose my intended location is
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/folder1/file1.xls";
which, in my session, resolves to
/var/mobile/Containers/Data/Application/035ECE7D-0E9F-4DF9-927B-B79FB31AEE01/Documents/folder1/file1.xls
Then I make sure the location exists
if (!Directory.Exists(filepath))
{
Directory.CreateDirectory(filepath);
}
which, according to Microsoft, should work
Instead of having the subfolder created and the file stream happily saving the file into it, the CreateDirectory method throws this:
{System.UnauthorizedAccessException: Access to the path "/var/mobile/Containers/Data/Application/035ECE7D-0E9F-4DF9-927B-B79FB31AEE01" is denied.
I thought MyDocuments was free to do stuff in? I've seen no documentation that says I have to apply for permission first. Where should I be creating folders?
If you code with Visual Studio, try to run the program in administrator (Visual Studio), after run your project. Maybe it’s the good solution.
You can try to create platform specific classes for working with saving files and use DependencyService for calling your methods. For iOS app I used this folder enum: Environment.SpecialFolder.Personal and that worked for me.

Xamarin's XDocument doesn't save files

According to this document, Xamarin should be able to write files in personal storage of application. Problem is, I used both XDocument.Save(string path) and XMLWriter to save my XML data to a file, but nothing happens! No Error but the file is not created either.
If you use another path, an exception will be thrown that you don't have access to that space, which means the code tries to write the file, but when you give it a legal space, it doesn't write anything.
I use:
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
to get my path and then add "/file.xml" to it. I give this path to
XDocument doc = new XDocument();
doc = XDocument.Parse("<names></names>");
doc.Save(path);
I tried this solution in windows and it works but not in Xamarin.Android
As for Saving, I was able to do it after giving my app WRITE_EXTERNAL_STORAGE. However it seems odd...since I could write files through File.WriteAllText in the same place...and my code never threw any exception about illegal access or whatever.
After this problem, another odd thing happened. XDocument.Save wrote the file, but later when it wanted to load it, threw exception about "XML Declaration"...the odd part is, XDocument.Save itself writes that part automatically and it can't be wrong!When I checked the file, it was completely OK.
So, I conclude that either XDocument is buggy in Xamarin (since I used the exact code in windows without any problem) or my android device is buggy. Either scenarios are unlikely.

Why does extracting file a file using TZipFile in iOS Simulator raise access violation?

I'm using TZipFile to extract a zip file and it's works ok in win32 but raise this exception in ios simulator. I dont know why , i've checked the location for the extraction is ok, passed the open file but when come to the extract it still raise that exception. Currently i'm not having any ios device for the real testing but please help on simulator, i'm frustrating with this.
ZipFile.Open(filePath, zmRead);//this line passed,
ZipFile.Extract(0,dirPath );//raise EAccess exception in this line
ZipFile.Close;
//the filePath and the dirPath is the location of file and location i want to extract, it's all correct.
Or use
Zipfile.ExtractZipFile(filePath,dirPath) //still that exception
Update:
OH i think i'm missing the information about my project, my working is to download a zip file which contain a .csv file from a server. I've downloaded it to the a folder(create at run time) in the ios simulator, the directory of folder i put in to variable dirPath = Tpath.GetHomePath() + SeparatorChar + 'csv' and the variable 'fileName' is the dirPath' + name of file zip i downloaded. And i about to extract it right in that folder. So i use TZipFile to extract it and it cause up the access violation error in ZipFile.Extract line. I putted my download and extract section code to a new project and it works perfectly. I dont know why but my main project is a large prj which contain many functions and come up before my download section.Thanks
Problem solved, in my Download.pas is a separated class and i use library System.Zip in there and i use another class to call it. So my work around is put uses System.Zip right in the Main form when project start and problem solved, there is no logical here and i think it's a bug from System.Zip. Thanks

How to avoid intermittent Errno::ETXTBSY exceptions?

During part of a request in a Rails application, I copy a directory from one place to another, think of it like a working area. Sometimes this copy operation results in "Errno::ETXTBSY" exceptions being thrown. I can't seem to pin down the case that causes it, any tips to detect the case or avoid it altogether?
I've made sure the destination directory is uniquely named, so it shouldn't be a case of 2 processes attempting to write to the same place. Beyond that I'm out of ideas.
ETXTBSY means that you're trying to open for writing a file which is currently being executed as a program, or that you're trying to execute a file which is currently open for writing. Since you say you're copying files, not executing them it seems likely it's the former, not the later.
You say you're targeting a unique new destination, but my guess is that's not entirely true and you're actually targeting an existing directory and one of the files you're attempting to overwrite is currently open as an executable text segment of a running process.
You haven't posted any code, so it's hard to comment specifically. I suggest you add enough logging so you know exactly what file(s) are being processed and specifically, the source and destination path that throws the exception. Then you could use lsof to see what process may have that file open.
One way to avoid the problem if you are overwriting a currently open executable, is to first unlink the target file. The running process will still have the old inode mapped and proceed merrily using the deleted file, but your open for write will then create a new file which won't conflict.

Need help opening printer spool shadow file (.SHD) that is locked

I'm interested in some information inside a shadow file (.shd) located inside the windows print spooling directory "C:\Windows\System32\spool\PRINTERS". Every time a print job is started, a spool file (.spl) and a shadow file (.shd) are created in that directory. So far I have been successful in detecting when a print job has started, and have been able to pause that print job. If you don't pause the job, the files eventually make their way to the printer and then are deleted by windows.
My problem is. I cannot open the .SHD files because they are locked in such a way that you can not read them while they are open by the sprint spooler. I've even tried going to the file in windows explorer and simply copying the file to another file, and that didn't work either. The .SPL spool files I can open though. I simply wait, and fairly quickly the spooler release that file. For the shadow file though, it permanently holds on to this file. Unfortunately, its the one I need.
The line of code I'm using specifically to open the file is as follows:
m_spoolJobStream = new FileStream(spoolFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
The IOException I get is:
The process cannot access the file 'C:\Windows\system32\spool\PRINTERS\FP00083.SHD' because it is being used by another process.
So yes, it is being used by another process. Its being used by the window's print spooler service. But I don't think there is anything I can do about that. All I want to do is read the file. I don't want to make any changes to it. Is there anything I can do here or am I just screwed?
Check the option: "Keep printed documents" (if you have HP printer) and then see your spool file folder, both shadow and spool files would be there.
Well, I did not find a way around this problem. I suspect there is no solution for this and it is by design. However I did find another way to get the information I wanted (at least it seems so thus far).
I'm using the FindNextPrinterChangeNotification() routine out of the winspool.drv library. This guy returns a pointer to a PRINTER_NOTIFY_INFO structure, which in turn contains an array of PRINTER_NOTIFY_INFO_DATA structures. Within that array, there is an element with its "Field" member marked as "JOB_NOTIFY_FIELD_DEVMODE". This element contains a fairly large structure of type DEVMODE. The structure is explained by M$ here http://msdn.microsoft.com/en-us/library/dd183565%28v=vs.85%29.aspx . This structure looks like it contains what I'm looking for and apparently is wrapped up in the .SHD file anyways according to this page http://www.undocprint.org/formats/winspool/shd. I'd like to know what else is in that .SHD file, but I still can't open it because its locked while the job is paused, and I suspect that it stays locked until the job is complete. Oh well, I think my new solution is more elegant anyways.
Just make sure you pause the job in the spool on BOTH your box and the server, then you should be able to copy/open/move the shd file just like you can the spl file. Worked for me, anyway...
This works for me:
- Hang your printer (e.g. jam the paper)
- Print and observe .SHD and .SPL being created
- Stop Print Spooler
- Open the file
The problem might be the FileShare.ReadWrite parameter. You're asking to read and write on the file and maybe that's why you get an error. You should try asking for read-only permission.

Resources