Change file extension into C#

Asked

Viewed 1,053 times

3

With Openfiledialog I am opening a file, and I want Load to change the extension. That is, I’ll put that the "openFileDialog1.Filter" would only be for ". txt" and I want it to change to ". cnf". Thank you.

  • What is o Load?

  • Not on Load, on Button @ramaral.

  • I can’t figure out what you want.

1 answer

2

I don’t know why this is, but a simple solution is for you to do it this way:

//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 of the file and add the cnf;

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

Or you can use a native function of C#:

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

See more about Path.Changeextension().

See Working

Browser other questions tagged

You are not signed in. Login or sign up in order to post.