Change file extension in C #

3

With an Openfiledialog I'm opening a file, and I want it in Load, change the extension. That is, I'm going to put the "openFileDialog1.Filter" would be just for ".txt" and I want it to change to ".cnf". Thank you.

    
asked by anonymous 25.06.2015 / 10:37

1 answer

2

I do not know why this, but a simple solution is you do like this:

//aqui vai o caminho que você leu no OpenFileDialog
string caminho = "arquivo.txt";

//e aqui você cria uma variável que irá receber o novo caminho.
string novoCaminho = caminho.Remove(caminho.Length -3, 3) + "cnf";

Basically what the above line does is remove the txt from the file and add cnf ;

Only you will have to save the file for it to have this extension.

Or you can use a native function of C# :

string caminhoPath = Path.ChangeExtension(caminho, ".cnf");

See more about Path.ChangeExtension () .

See Running

    
25.06.2015 / 12:43