What process is Streamreader using? - dispose

I made a simple program with
StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName));
txtBox1.Text = sr.ReadToEnd();
and forgot to put
sr.Dispose();
and now when I try to run the program and open a file I get the IOException was unhandled error message that says "The process cannot access the file because it is being used by another process." So my question is, does anyone know what process is it using? I would like to be able to find it in the taskmanager and end it instead of writing a bunch of exception handling code since this is just a program that I'm using for practice.
The file I tried to open is a txt file in MyDocuments.

The process that is using the file is the process that opened it which is very likely .vshost.exe if you are running this from from Visual Studio.
To avoid 'forgetting' to dispose of objects in the future using the using statementfor anything implementing IDisposable (which StreamReader does). Your code would look like
using(StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName)))
{
txtBox1.Text = sr.ReadToEnd();
}

I got the same error when I was using a streamreader to find out the end of the text in the file so I could then add the new text after the text in the file with the writeline method. My Documents has accessibility issues when it come to development anyway. Try putting the .txt into some other location maybe /temp folder?

Related

Writing string to specific dir using chaquopy 4.0.0

I am trying a proof of concept here:
Using Chaquopy 4.0.0 (I use python 2.7.15), I am trying to write a string to file in a specific folder (getFilesDir()) using Python, then reading in via Android.
To check whether the file was written, I am checking for the file's length (see code below).
I am expecting to get any length latger than 0 (to verify that the file indeed has been written to the specific location), but I keep getting 0.
Any help would be greatly appreciated!!
main.py:
import os.path
save_path = "/data/user/0/$packageName/files/"
name_of_file = raw_input("test")
completeName = os.path.join(save_path, name_of_file+".txt")
file1 = open(completeName, "w")
toFile = raw_input("testAsWell")
file1.write(toFile)
file1.close()
OnCreate:
if (! Python.isStarted()) {
Python.start(new AndroidPlatform(this));
File file = new File(getFilesDir(), "test.txt");
Log.e("TEST", String.valueOf(file.length()));
}```
It's not clear whether you've based your app on the console example, so I'll give an answer for both cases.
If you have based your app on the console example, then the code in onCreate will run before the code in main.py, and the file won't exist the first time you start the activity. It should exist the second time: if it still doesn't, try using the Android Studio file explorer to see what's in the files directory.
If you haven't based your app on the console example, then you'll need to execute main.py manually, like this:
Python.getInstance().getModule("main");
Also, without the input UI which the console example provides, you won't be able to read anything from stdin. So you'll need to do one of the following:
Base your app on the console example; or
Replace the raw_input calls with a hard-coded file name and content; or
Create a normal Android UI with a text box or something, and get input from the user that way.

VSCode - auto_prepend_file for code suggestions

I have been using VS code instead of eclipse for the last few weeks and find it much quicker, easier to use. However there is one thing I can’t seem to figure out.
My php app calls a prepend file which initiates a class called GlobalClass:
$gc = new GlobalClass();
When I type $gc-> in eclipse I get all the functions prompted. This doesn’t work within VS Code.
If I add use GlobalClass or add the $gc = new GlobalClass(); to the top of the file then it works. Is there any way to declare this within VS Code or to point the editor to the code within the prepend file?

Java (Mac OS) : Writing my object to a file results in garbled test when re-reading the object from the file

I have written the following code using the Eclipse IDE.
When I open the txt file with gb18030,it is some of the test is garbled, as can be seen here:
Any suggestions on how to handle this issue?
Of course, because you are using ObjectOutputStream, try
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("path", false), StandardCharsets.UTF_8);
Check out the answer at Serialzed Objects Stored in File are not readable
and JavaDoc

Automatically coming slashes with FileStream in Monodroid

I have been working on a project using Monodroid. I need to use FileStream to access a file in my project. I write the path as a first argument in FileStream like FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) but two slashes come automatically from FileStream, therefore the path is not valid. For example, if the path is "res/raw/aFile.txt", after FileStream method it becomes "//res/raw/aFile.txt" and an exception occurs. Because of the exception, I cannot split and get the valid path. How can I get rid of the two slashes that automatically coming from the FileStream method?
Regards.
If you're trying to open a raw resource file, I would suggest using the OpenRawResource() method instead:
using (var stream = Resources.OpenRawResource(Resource.Raw.File))
{
}

How send a file in email without saving file to disk?

I need to send a vcal file via email. I want to send the file without creating it on disk.
I have the file in string format.
Here's what I think you are looking for in C#
System.IO.StringReader stream = new System.IO.StringReader("xyz");
Attachment attach = new Attachment(stream);
MailMessage msg = new MailMessage();
msg.Attachments.Add(attach);
SmtpClient client = new SmtpClient();
client.Send(msg);
"xyz" should be replaced with the string of the attachment. The code above will allow you to add an attachment to a MailMessage object without ever having to retrieve that object's data from disk, which is what I think you meant instead of 'memory'.
Probably the best you can do (if you're using a graphical email program) is just save the .vcal file to a temporary directory, then pass that path to the mail program.
You can generate temporary filenames with functions like mktemp.
I'm not sure what you mean by not 'creating it memory'. Operating systems work by reading any file from disk into in-memory buffers. So, chances are that the file will still reside in memory at some point. Furthermore, when you pass the file over to the programme or tool that will dispatch it, it will also pass through some memory buffer.