Txt file not opening - c#

**Hello, my Visual C# is running but when I enter a person's name in Form1.cs[Design] it won't open the file. My code is below and I'll also attach how I saved my txt and what errors are showing. I been trying to figure this out for hours. My goal is to learn how to save txt in my directory folder and how to call it in the .cs code.
`` ```
Errors
enter image description here

tldr;
Your filename is 'BoysNames.txt.txt'.
From the image it is clear that the exception occured, because there is no file called 'BoysNames.txt' in the current directory. I see what the problem is. You have hidden the extension of files and still the text file is displayed with its extension. (Note that since the 'exe' file doesn't show its extension but 'txt' is showing. It means it has double extensions). So, you need to do this:
boysFile = File.OpenText('D:\\visual c#\\pp22\\bin\\Debug\\BoysNames.txt.txt');
To avoid this type of problems in the future, try these steps.
Go to Control Panel.
Click on File Explorer Options -> View
Uncheck this Option 'Hide extensiobns for known file types'

Well it looks like it will not open the file because it is not where you are saying it is. Look in the error, it tells you where it is looking for the file, is it really there?

Related

How do I have my code trigger an event when a file is added to a given folder?

I am trying to have my software run a command line instruction whenever a certain type of file is added to a given folder.
So far I have the ability for a user to select the folder, and the path is saved to a string.
I am just wondering if there is some kind of event that could be called when a change is made to that folder?
(c#)
I think what I was looking for is called 'File System Watcher' and a guide can be found here:
https://www.youtube.com/watch?v=6lVormV8cb8

C# Open a process without know location path

I have to open a process.exe from a button_click event by known only the filename and the .exe extension.
Is It possible? In two words i need to make a game.exe library ,and if the filename.exe will be found the process filename.exe,
need to be started.
I want to avoid to ask the costumer the full path of the program location, and i dont want to use OpenFileDialog()for browse. Im three days stuck here. So
filename.exe=textbox1.text
SystemDiagnostic.Process(..).Start(filename.exe)
this will open only the file if the file and the debug program are in the same directory.
Do i need a recursive search, and if the file is found open the process?How do i search for the filename.exe in all the #"C:\?
Thank everyone Sorry for bad english.
If that file is deployed by you, like it is up to you to make an installer for it, you can add the containing directory of that file to the PATH environment variable, then you can simply launch it by
Process.Start(“filename.exe”)
Without having to specify the full path to that file.
If it is out of your control, but there is some trace about the location of that file, for example, if it is from a known vendor and the file has a default location or there is something like “InstallLocation” in registry for that file, you can query registry to get the full path then launch it.
Your last resort will be scanning the whole disk for that file. But this is too bad, unreliable and slow...and things can go wrong easily. I strongly warn you against this approach.
If you have to choose the last approach, you might find this post useful. Remember to check other attributes like version number, publisher, etc., in case you end up calling a wrong file happens to have that name.

Process.Start, WorkingDirectory, start one exe from another

I hope you will get my problem, dont know if I can describe it properly in english, but I will try :)
Situation:
Folder Structure: MainFolder/Sub1/Sub2
2 exe files: MainFolder/MainProg.exe and /Sub2/SecondProg.exe
MainProg.exe is not from me, I wrote an .cs file inside /Sub1, kind of a plugin. (I think MainProg.exe kind of works like a compiler, cause it has a log window where I can see errors from my .cs file.. I think the author somewhere mentioned a JIT compiler ?!)
Start MainProg.exe -> Click a button -> Start SecondProg.exe
SecondProg.exe reads values from an xml file
Both are .NET 4.5
Problem:
I want to have the xml file inside /Sub2
If I do that and start SecondProg.exe the normal way, double clicking it, everything is working fine
If I try to start SecondProg.exe via the button in MainProg.exe, I get "SecondProg is not working anymore" from windows.
If I copy the xml file to MainFolder, its working..
So, I am still learning C#, but could there be a problem with the workingdirectory? I am so confused, because the MainProg.exe has nothing to do with my xml file, it doesnt even know its there, the only point, where I use it is when loading values into SecondProg.exe...
Inside my .cs file, I start the SecondProg via
public override void Button()
{
Process.Start("Sub1\\Sub2\\SecondProg.exe");
}
So its like, MainProg has the button, in my .cs file I tell him what to onClick.
well.. its hard to describe if you're not using your native language, but I hope you get what I mean ;)
That SecondProg.exe apparently looks in its working directory for that file and fails if it doesn't find it. As such, you have to set the working directory of the new process. To do that, create a ProcessStartInfo object, set the FileName to the path of the EXE file and the WorkingDirectory to the path of the folder containing that EXE. You then pass that object as an argument when you call Process.Start.

Opening an image file in an executable I created

I just finished this tutorial - Create a Picture Viewer - on the MSDN site, and it all works well according to the tutorial specifications (although I did remove the unnecessary buttons).
At the moment, I can open an image file from within the program, which is fine.
However, I'd like to be able to open an image file from Windows Explorer, and have it open in my Image Viewer. (Using the Open With.. context menu).
I did try and open it via the Open With.. menu, but when the program loaded, the image didn't show up. The program just started up as it normally would.
What code do I need to put in, that allows me to open the program via an image file (if that makes sense)
You can use:
Environment.CommandLine
Which will contain stuff in the format of "..." "...", first being the path of your application, and in your case - second would be the path of the opened-with file.
Then, you can split that to get the second "..." and load the file as you normally do in you application.
You can also check to see whether the arguments of the application contain the path of the opened-with file. I'm not sure about that, but it should be very easy to check: Have an mbox which prints the parameters, then try opening a file with your program and see what shows up. Using the args[0] or args[1] or whatever will probably be easier than splitting Environment.CommandLine...

Reading and writing to files

I have a few questions. Yes this is homework and I am just trying to understand it.
This is what is being asked.
• When the button “Load” is clicked, read the file specified in the textbox (txtFilePath: Absolute path not relative) and add the objects found within to the listbox
• When the user clicks the “Save” button, write the selected record to the file specified in txtFilePath (absolute path not relative) without truncating the values currently inside
Can someone please explain to me as I am still learning this. I have the button and the textbox there and the same with the save. Now with the save button will I just have the same code as you would if you just wanted to save it. But from what I am gathering there is a database so you can load the file that you saved. Or am I making this harder than what it is?
No, no database. In these instructions, record == some selected item that needs to be appended to an existing file. Just use a stream and a writer to save the file to disk and you satisfy the requirement.
No, there is no database. What you do is interact with the Windows file system (eg, the files on your hard drive). You use the classes in the System.IO namespace to load and save files.
'Absolute path' refers to the unique location of a file in the drive expressed as a rooted expression; a 'relative path' is a partial path that points to a file relative to a given location:
c:\foo\bar\baz\my files\homework.txt
..\..\homework.txt
Those are an absolute and relative paths.
I'm not sure how much detail you are looking for here, it's hard to give a complete overview of the way filesystems work. You might want to look at the basic examples in MSDN that deal with file management.
It's hard to give a detailed analysis of this subject as it is quite a wide topic.
For file interaction you must use the System.IO namespace which has classes to easily load and save files.
http://msdn.microsoft.com/en-us/library/system.io.aspx
The link above is a good reference on MSDN on how you can get started with File Management using System.IO.
Good luck!
If I understand you correctly, your question is wether or not you need to read the file a second time before saving or otherwise treat if differently than if you created a new file.
I would say "no". You have already read the content of the file into the listbox. You just need to get the edited content from the listbox (when the user is done with it) and save it to the file (overwriting whatever is there).
First of all read up on how to read and write files. Here's a good link I found:
check it out
Next what you'll want to do is put your read/write code in the Button_Click event of each button (double click on your buttons to auto create this event assuming your using Visual Studio)
You can easily retrieve the path from your text box by accessing the .text() property of your textbox
string path = myTextBox.Text;
It's been a while since I've coded anything in c# but this is pretty basic and I think it should help.
For Load:
Read the file line by line
Add each line to the ListBox Items
For Save:
Open your save file without truncating (ie append to the file)
For each item in your ListBox Items, write it to the save file

Categories