So I have made my own custom Notepad program everything works just like a normal notepad but now I want to launch it from .txt files. I change my default launch program for .txt files to my NotePad2.0. The problem is when I i launch the from a .txt file it doesn't load in the text. Anyone got any ideas? I'm looking for a possible file path to the .txt file or some other way that I can pull the text out of the .txt file on open.
(other details: I am talking about when I am in a File Explorer or on the Desktop and I open a .txt file. I want the text that is in that .txt file to load into my program.)
Thank you to anyone who helps/looks over my problem!
Use the Environment.GetCommandLineArgs method to retrieve the filename which is opened and then read all the text inside your file using the File.ReadAllText method and load the resultant string into your app (maybe in a textbox):
textBox1.Text = System.IO.File.ReadAllText(Environment.GetCommanLineArgs()[1]);
Where textBox1 is the text container in your app.
After that, if you open a text file with your app, you will see that all its text is properly loaded into your TextBox.
Note that the first argument is the name of the executable itself. Thus you need the second argument. Also, you might want to check if there is a second argument because otherwise it will crash if you directly open the executable. You might also want additional checks like if the file is a text (.txt) file or not and so on.
For that, you can do something like this:
var args = Environment.GetCommanLineArgs();
if (args.Length > 1)
{
textBox1.Text = System.IO.File.ReadAllText(args[1]);
}
Related
I'm starting a new project, and I need to be able to save/load users data.
I want them to click a button, then this brings up a file explorer where they select their .txt file. This .txt file then needs to be stored in a string array, where I can then manipulate the data.
If someone could point me in the right direction of what tools to use, that would be great. I've looked at OpenFileDialog, but I don't see how to assign it to an array.
Sounds like you're just looking for File.ReadAllLines - once you've got the filename, of courses. You need to separate out the tasks of "let the user select a file" from "read the contents of the file".
OpenFileDialog is probably the right tool to use for the first part, then File.ReadAllLines for the second...
I am trying to read the file properties, For example If i change the file extension of test.txt file into test_txt.vsf, the type of file still .txt in file properties. I want to read this file extension from properties.
I am usinf below code which displays the file name extension as .vsf. But actually it's extension is .txt.
FileInfo info = new FileInfo(#"C:\Users\saravana_rajkumar\Desktop\Test_txt.vsf");
Console.WriteLine(info.Extension);
Please guide...
The type of the data actually in a file is not stored anywhere by Windows. It is up to applications to determine if they can handle a file they are given.
For example, if you rename an EXE to ".txt" you can try to open it with Notepad, and it will try to open it as a text file.
When you say this:
If i change the file extension of test.txt file into test_txt.vsf, the type of file still .txt in file properties.
You are wrong. The type of the file is not still ".txt" in file properties. The file properties for file type in Windows Explorer works solely off the file suffix.
Do you tried to use Path.ChangeExtension method ?
Check out Path.ChangeExtension documentation in MSDN
Example:
string newFileName = Path.ChangeExtension("test_txt.txt", ".vsf");
I have a C# Windows Forms application where I load either a XML file or a CSV file for some task operations. When I click the Browse button I have, an Open File Dialog box appears and I can navigate to a location on my drive and choose the file and then upload it using an Upload button.
If I load a JPG or a ZIP file or any file whose format is anything except CSV or XML, my application crashes. Is there any way of limiting the Open File Dialog box to open only CSV or XML files alone in C#?
Use
openFileDialog.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";
this way only csv files or xml files are shown.
Odd-numbered pipes delineate between what's visible in the Filter dropdown and the corresponding actual file extension, and Even-numbered pipes delineate between the first entire file extension and the second.
For example, "CSV files (*.csv)|*csv" means users will see "CSV files (*.csv)" in the filter dropdown, and that option will look for any files that match *.csv.
In the line of code above, the pipe before "XML" indicates an entirely new filter option that will appear below the CSV option.
Nevertheless, users can also select other filetypes if they type in the complete name - so check the filename that was selected and correct your code accordingly.
You can use the Filter property to let the user choose a certain type of file.
However! This is not a guarantee. A user is still able to input '(star).(star)' in the filename box and show all files. So you should check the resulting file(s) in your code as well.
You can do this with the Path.GetExtension() method.
You can apply a filter in your Open file dialog which only shows .xml and csv files as mentioned above.
With path.getextension http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx You can check if the user indeed selected a file with the right extension. If a wrong extension is selected, you can prompt to select a different file.
I would strongly recommend to check the file extension before upload. Just check the extension after the user had selected the file. If the wrong files was selected, just don't continue the upload/processing...
Ive been trying for a while now to get a rich textbox to populate from a text file. Normally I would say this is easy and there are tons of solutions on here for it. My only issue is I don't want the open file dialog to populate it I want it to be populated on the onload event of the form, and populate from a known text file on the network drive here. Every solution I find seems to be asking for the users input to open the file I don't want this. This is due to the fact that when the text file is edited( which it will often be) I want the textbox to mimic this rather then have an outdated version hard coded in. Please help
If you know the location then
const string Myfile = #"c:\test\myfile.rtf";
richTextBox1.Loadfile(Myfile);
you can use UNC etc, but it may error if it cant find the file - so I would suggest file checks etc.
Use System.IO.FileSystemWatcher to watch for changes to the file, and then System.IO.File.ReadAllText to read the contents of the file into your textbox.
There is an existing process that is writing to a text file on my site. Let's say the file is at http://www.mysite.com/addresses/addresses.txt
I have been able to successfully display the contents of the file in a text box using client.DownloadString, however I can't find a clear answer on how to append to the end of this text file and also to clear the contents of this file. This is a text file on my web server.
I'm working in C#. I do know the path to the file on the server at c:\inetpub\site\addresses\address.txt as well.
Any ideas on how to append or clear this file? Everything I seem to find on it is on windows forms and i don't use enough DotNet to know where to look.
Thanks!
Check out this Article CodeSnip: Read and Write Text Files with ASP.NET 2.0
System.IO.StreamWriter StreamWriter1 =
new System.IO.StreamWriter(Server.MapPath("test.txt"));
StreamWriter1.WriteLine(TextBox1.Text);
StreamWriter1.Close();