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");
Related
I have in the resources of my visual studio C# projetc a xlsx file and I want to manipulate it by FastExcel library (https://github.com/mrjono1/FastExcel), but, like see you in the github's page code, I have to create a FileInfo object and it has only one constructor that wants a file's path but only link that I have of the file is the stream that I get by this line of code:
test.Properties.Resources.test1
How do I pass throught from link to resources to path for create a FileInfo?
Well you can write the Stream to disk and provide the file path to the FileInfo constructor. I would create a seperate utility that can accepts a Stream and returns a file path.
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]);
}
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...
How do you check a file type when there is no extension in c#
For instance, I have files with no extension, that are either .mp4 or .flv format (just no extension). I plan on converting these video files to audio files however I would like to determine the file type before I start converting it. Is there a way to do this in C#?
I was thinking that maybe I could just rename the file to name.mp4, then perform some task on the file that would either
A) succeed, meaning that the file was indeed .mp4, or
B) fail, in which case I could then rename it to .flv
then convert the file as the appropriate extension. Is there a native process in c# that can look at .mp4 properties or .flv properties? I do not want to rename the file to .mp4 and then open it in a third party application, such as Windows Media Player, in order to see if I named it correctly.
I've heard of reading the first few bytes of a file's contents and making an educated guess at the file's format. This link seems promising:
Using .NET, how can you find the mime type of a file based on the file signature not the extension
I had played this utility (TrID - File Identifier) and seems quite accurate. File type defination package (TrIDDefs) is also up to date.
And Here is a list of file type signature table if you interest. The list is continuing work-in-process.
I want to select multiple files based on their specific contents from a particular folder and edit their contents. I am using winform in C#.
any idea which are the classes that can be used.
It would be helpful if code given for example.
thanks.
have a look at Directory.GetFiles to get to the names of the files in a directory. If they're text files you can read them via File.ReadAllLines (returnsan array of strings, one per line of the file) or File.ReadAllText (returns a single string containing the entire content of the file).
To save the edited files have a look at File.WriteAllLines or File.WriteAllText.