how to join 2 path file

# how to join 2 path file

• Tuesday, March 27, 2012 5:58 AM

hi!

I'm uing code c#

I'm doing an example transfer file throw network. I want client send file name to server then server join this name and a path folder default to a path file in server but I cannot do that. when I do it in main method it ok but when I do it in an other method then call it not work

example:

 private void sendExam(string name)
{
string n = @"\" + name;
string path = Ultil.dirServer+n;

//tại đây mình in ra cho kết quả đường dẫn đúng(D:\Test\Server\aa.pdf)
Console.WriteLine("path: "+path);

if (File.Exists(path))
{
tf.sendCM(Ultil.ok);
tf.sendFile(path);
}
else
{
tf.sendCM(Ultil.fail);
}

}

I tryed use

string path = Path.Conbine(dir,namefile)

or

string path = String.Concat(dir,namefile)

but all of that not work. when I use File.Exists(path) alway is not exist. (I tryed check exist by FileInfor and it throw an error "Illegal characters in path")

help me!

Thank you

### All Replies

• Tuesday, March 27, 2012 7:07 AM

You can try removing the @"\" character.

I believe it is adding an extra \ into the path.

Your new code should look like:

string n = name;
string path = Ultil.dirServer+n;

Resolving n Evolving in C# (http://jeanpaulva.com)

• Tuesday, March 27, 2012 7:32 AM

You can try removing the @"\" character.

I believe it is adding an extra \ into the path.

No, its not. when using "@" simbol, it actually takes characters as they are literaly.

The same is:

- @"\text";

- "\\text";

The output is "\text"

--

But I would suggest you not to use any kind of path structure with backslashes, alwayw use Path.Combine (this automatically addes backslashes).

I would say your problem lays in "name" variable.

--

Here is an example code:

string fileName = "testFile.txt";
string dirName = @"C:\Myfolder";
string fullPath = Path.Combine(dirName, fileName);

//output will be:
//C:\MyFolder\testFile.txt

Mitja

• Tuesday, March 27, 2012 7:33 AM

Yes Mitja, You are right.

I have tested in myself.

Resolving n Evolving in C# (http://jeanpaulva.com)

• Tuesday, March 27, 2012 7:35 AM

Try backtrack approach, first hard code the path in your path variable. When the path is valid, that is worked only then generated it dynamically. What really happens is that we try to create paths dynamically and try to use that, for sure it doesn't work in many attempts. Try this and if you still have issue please get back.

Thanks, Kashif

• Tuesday, March 27, 2012 8:27 AM

You can try removing the @"\" character.

I believe it is adding an extra \ into the path.

No, its not. when using "@" simbol, it actually takes characters as they are literaly.

The same is:

- @"\text";

- "\\text";

The output is "\text"

--

But I would suggest you not to use any kind of path structure with backslashes, alwayw use Path.Combine (this automatically addes backslashes).

I would say your problem lays in "name" variable.

--

Here is an example code:

string fileName = "testFile.txt";
string dirName = @"C:\Myfolder";
string fullPath = Path.Combine(dirName, fileName);

//output will be:
//C:\MyFolder\testFile.txt

Mitja

I tried it.
when I use in main method  it work, in method sendExam(string nam) in class server it not work :(

(I printed path into console it ok.)

• Tuesday, March 27, 2012 8:43 AM

Try backtrack approach, first hard code the path in your path variable. When the path is valid, that is worked only then generated it dynamically. What really happens is that we try to create paths dynamically and try to use that, for sure it doesn't work in many attempts. Try this and if you still have issue please get back.

Thanks, Kashif

When I try

   if (File.Exists("D:\\Test\\exam.txt"))
{
tf.sendCM(Ultil.ok);
tf.sendFile(path);
}
else
{
tf.sendCM(Ultil.fail);
}

i ok

but when join path two file it not work

• Tuesday, March 27, 2012 9:12 AM

Thank you all of you.
I know reasons of that error now. it not wrong in join 2 path. I have mistake when recieve name file throw network.

it ok now. thank you for help me. :)

• Friday, March 30, 2012 6:08 PM

I though it wasnt that error too.

I am glad you found the cause of it.

Mitja