0

I am using C# .net and I am trying to upload a file to my server. I am not using asp .net.

I followed This Question and it doesn't seem to work for me. On a sidenote, this question was written back in 2008.

I am using this code as shown in the question above:

File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\public_html\\new");
//tried this also
File.Copy("zQn69PZUx.png", "\\\\198.57.247.152\\~shiro\\new");

The error I get:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The network path was not found.

I also tried using my domain name and use the path like it shows up in the browser, without http:// because it complained about the format, saying its not accepted, like this: domain-name.com\\new, and still got the same error as a result.

Any ideas what I could be doing wrong, or have a better solution on how to upload a file to a server ? You can see here that the http://198.57.247.152/~shiro/new/ path exists.

Community
  • 1
  • 1
  • Is `198.57.247.152` a computer in your local network or do you only have access to it remotely? – Peter Luu Apr 21 '15 at 01:14
  • @PeterLuu It is a shared server I have been renting from a company named hostgator. This is why the `~shiro` subfolder is for my personal use – dimitris dimitris Apr 21 '15 at 01:15
  • @Sayka What do you mean by `sample` ? This is a valid link http://198.57.247.152/~shiro/new/ works on the browser – dimitris dimitris Apr 21 '15 at 01:17
  • Have you setup the FTP from your hosting control panel? – Abdul Saleem Apr 21 '15 at 01:17
  • If it's a shared server from a company, do you have the FTP credentials available to you for that server? – Peter Luu Apr 21 '15 at 01:17
  • @PeterLuu Yes I do have FTP privileges, if that's the question. How else would I be able to upload my site to the server otherwise ? I can connect with Filezilla and upload files normally. – dimitris dimitris Apr 21 '15 at 01:19
  • You copy files to Filezilla using this code? If yes I'll check and upvote your question. Cauz i've never seen anywhere using File.Copy for uploading.. – Abdul Saleem Apr 21 '15 at 01:21
  • @Sayka That's not what I meant. I replied to Peter Luu saying that I can upload files using the Filezilla client (through the interface of course), therefore I have FTP privileges I suppose. – dimitris dimitris Apr 21 '15 at 01:22
  • I was more asking if you had the username and password available to you - which in this case would be yes if you can do that – Peter Luu Apr 21 '15 at 01:23
  • That server isn't even running windows. You won't be able to use File.Copy() to an smb share. – Joel Coehoorn Apr 21 '15 at 01:38

2 Answers2

3

The path \\198.57.247.152\~shiro\new is what Microsoft calls Uniform Naming Convention (UNC). This type of resource is only available on networks with NetBIOS enabled; essentially local network. The problem is that File.Copy only works if you have access to it in your network - since this is a remote server, it won't be able to find that server, leading to the The network path was not found exception.

http://198.57.247.152/~shiro/new/ follows the syntax of <scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ] which is call Uniform resource locator (URL). Hypertext Transfer Protocol (http) resource is typical accessed by a browsers.

You can resolve this by instead using FTP to upload your file to the server:

using (WebClient client = new WebClient())
{
     client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
     client.UploadFile("ftp://ftpserver.com/targetpath", "localfilepath");
}

A valid target path would be something like: ftp://localhost/samplefile.txt and the local filepath should be the fully qualified filepath.

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Peter Luu
  • 446
  • 2
  • 10
  • Yes - I'm not sure if your ftp address is the same as the 198.57.247.152 address, so I was using placeholders. – Peter Luu Apr 21 '15 at 01:29
  • This seems to be working, kind of. I am getting this error: `the remote server returned an error: (553) File name not allowed.` I am contacting my server hosting company right now to see what the problem is. Thanks for the answer – dimitris dimitris Apr 21 '15 at 01:32
  • Have a look at this in case it helps you http://stackoverflow.com/questions/9418404/cant-connect-to-ftp-553-file-name-not-allowed – Peter Luu Apr 21 '15 at 01:34
  • Yes there has to be something about the path. Problem is I don't know the actual path since I don't have access to all the directories, because its a shared server. Hopefully I will figure this out by asking them. – dimitris dimitris Apr 21 '15 at 01:36
  • ok the problem is that it needed the file name in the end as well ! (something.png for example) – dimitris dimitris Apr 21 '15 at 02:02
0

You can upload file using code from Peter Luu's answer. But first you should have access to it.

For uploading a file to a remote server, you should be a user of it and should have a password. It is not the password you use for logging in to your Hostgator control panel. After logging into your Hostgator account, there will be an option like FTP in it, where you can setup FTP user accounts. There you can assign a user name and password and that have to be applied to the code for uploading..

And for checking whether the UserName and Password works, Open MyComputer, and in the addressbar type the FTP path (which will be something starting "ftp://"). There pops up a Dialog asking for UserName and Password (If the path is valid). If you enter into, See and copy files into the explorer window from there, then the uploading code will work

enter image description here

If you want to get the applicable path, Open windows explorer and type ftp://your_domain_name. Apply UserName and Password, browse through the path you need, copy it from the address bar and add it to the code.

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
  • The account definitely works because if I put a wrong password/username I get a different error inside VS2013 – dimitris dimitris Apr 21 '15 at 01:53
  • Yes i figured out the problem 1 second ago ! it needed the file name in the end as well in the ServerFilePathLocation ! – dimitris dimitris Apr 21 '15 at 02:03
  • @dimitrisdimitris, if you want to get the applicable path, Open windows explorer and type ftp://your_domain_name Apply UserName and Password, browse through the path you need, copy it from the address bar and add it to the code. – Abdul Saleem Apr 21 '15 at 02:03
  • @dimitrisdimitris precede ftp:// to your domain name – Abdul Saleem Apr 21 '15 at 02:06