0

how can I save .pdf file to remote server? what to change at below code part?

if (File.Exists(@"C:\\Users\\xxx\\xxx.pdf"))
{
    MessageBox.Show("xx.", "xx");
}
else
{
    Directory.CreateDirectory(@"C:\\Users\\xx);
    PdfWriter.GetInstance(pdfDosya, new FileStream(@"C:\\Users\\xx\\xx.pdf", FileMode.Create));
}
Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
R.Pandinus
  • 31
  • 3
  • 1
    Please care to add a comment as to why the question is down voted so that the OP can edit the question and make it more meaningful. Simply down voting and moving on seems to be rude. – Kishore Jun 01 '18 at 13:36
  • 2
    You don't need to use double backslashes in your path name. The '@' makes it a verbatim string literal so escape characters are ignored. See [here](https://stackoverflow.com/questions/3311988/what-is-the-difference-between-a-regular-string-and-a-verbatim-string) – Lucax Jun 01 '18 at 13:42
  • I am just new at this so sorry about any insufficient explanation. – R.Pandinus Jun 01 '18 at 13:56

2 Answers2

0

Confirm you can view the place you are looking to save to. Then update your code

String serverLocation = @"\\servername\";

if (File.Exists(serverLocation +@"Users\xxx\xxx.pdf"))
{
    MessageBox.Show("xx.", "xx");
}
else
{
    Directory.CreateDirectory(serverLocation +@"Users\xx);
    PdfWriter.GetInstance(pdfDosya, new FileStream(serverLocation +@"Users\xx\xx.pdf", FileMode.Create));
}

As mentioned in comments, when you use @ you do not need to escape your \'s.

esre
  • 87
  • 9
  • To add on to this. If you map the network drive you can reference it using it's drive letter. For instance, "Y:\" – Lucax Jun 01 '18 at 13:46
  • 1
    This is true but if you're deploying to a server you'll need to make sure the correct drive is mapped or you won't know where it's being saved to. I would suggest having the server you wish to save to saved in a config file incase you need to update it between environments. – esre Jun 01 '18 at 13:48
  • I tried as below but I receive access is denied even though folder at server is shared accordingly. – R.Pandinus Jun 02 '18 at 13:49
0

Probably, the only difference would be your authentication into the server before save any file.

The easiest way to do that, would be using "User Impersonation".

Install it in your application

https://www.nuget.org/packages/UserImpersonation/

Install-Package UserImpersonation -Version 1.0.0

A Quick example with "User Impersonation DLL"

using (UserImpersonation obj = new UserImpersonation("YourUserNameToAccessTheServer", "YourDomain", "UserPassword"))
{
   string YourDirectoryFullNameExample = "\\ServerName\\Folder1\\Folder2\\File.Pdf"
   if (File.Exists(YourDirectoryFullNameExample))
   {
      MessageBox.Show("xx.", "xx");
   }
   else
   {
      Directory.CreateDirectory(@"YourDirectoryFullNameExample);
      PdfWriter.GetInstance(pdfDosya, new FileStream(YourDirectoryFullNameExample, FileMode.Create));
   }
}
Mario Guadagnin
  • 476
  • 7
  • 17
  • I tried as mentioned : An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the path 'xxxx__.pdf' is denied. – R.Pandinus Jun 02 '18 at 15:16
  • UserImpersonation obj = new UserImpersonation("xxx", "xxxx", "xxxxx"); string server = "\\server\\d$\\kontrol\\".pdf"; if (File.Exists(server)) { MessageBox.Show("yyy.", "yyy"); } else { Directory.CreateDirectory(server); PdfWriter.GetInstance(pdfDosya, new FileStream(server, FileMode.Create)); – R.Pandinus Jun 02 '18 at 15:23
  • @R.Pandinus Is your code inside of the "using(UserImpersonation...)"? Are your credentials valid? Try to manually create a .txt file inside your server directory, if it works, create another text file in your local machine and copy it from your local machine then paste it into the server using the code above, or like this example: using(UserImpersonation a = new UserImpersonation(YourCredentials)){ File.Copy("Your machine Path like C:\Root\Foo.txt", "\\myservername\Directory\Bar.txt") }. Also try to run Visual Studio as Admin – Mario Guadagnin Jun 02 '18 at 16:57