1

Suppose I have string variable string id='sample'. I want to make a file named sample.txt in my current directory (Environment.CurrentDirectory) using the string variable and after that I want to get the full path of the text file. How will I do that? Normally we can create a text file(say: sample.txt) in current directory in this way:

    string path=Directory.GetCurrentDirectory();
    string FileName = path + "\\sample.txt";
    File.Create(FileName);

But how will I do this using the string variable and after creation how will I get the full path of the sample.txt file?

2 Answers 2

1

Use the Path.Combine method

string path=Directory.GetCurrentDirectory();
string FileName = Path.Combine(path, id + ".txt";
File.Create(FileName);

and at this point you have already the full path name in the FileName variable but you can use other methods of the Path class

string fullPathName = Path.GetFullPath(FileName);
Sign up to request clarification or add additional context in comments.

Comments

0

I think this may help:

string path = Environment.CurrentDirectory() + "\\" + id + ".txt";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.