0

I need to upload files dynamically with c# . I can do it with asp.net but O couldn't do it with a desktop app.

I am getting the file to uploaded with open file dialog. Here is my code

string path = "";
        OpenFileDialog fDialog = new OpenFileDialog();
        fDialog.Title = "Attach PMI document";
        fDialog.Filter = "PDF docs|*.pdf|JPG Files|*.jpg|JPEG Files|*.jpeg";
        fDialog.InitialDirectory = @"Desktop";
        if (fDialog.ShowDialog() == DialogResult.OK)
        {

            fileName = System.IO.Path.GetFileName(fDialog.FileName);
            path = Path.GetDirectoryName(fDialog.FileName);
            textBox1.Text = path + "\\" + fileName;


        }

no problem with open file dialog.

When i try to save with this code to my computer it was succesful

       AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, password);
WindowsImpersonationContext context = idnt.Impersonate();
File.Copy(@"\\192.100.0.2\temp", @"D:\WorkDir\TempDir\test.txt", true);
context.Undo();

But when i try to copy file to network it gives "error providing a user name is not properly formed account name"

How can i copy that Thanks.

enderaric
  • 142
  • 4
  • 15
  • 5
    `"F\"` missing a colon? You have a route to 192.168.2.92? What happens if you start -> run -> \\192.168.2.92\Ender\Files\ – Alex K. Jan 19 '15 at 16:12
  • Good catch @AlexK. I entirely missed that! – RvdV79 Jan 19 '15 at 16:20
  • @AlexK. F is my local disk. And 192.168.2.92 is my server computer's IP. my computer has logged in to server with username asd\ender and password ender . – enderaric Jan 20 '15 at 14:58
  • Please remember to change your credentials now. That should never be made public. Some lads are quite handy with that you know... – RvdV79 Jan 20 '15 at 22:21
  • Thanks for updating, have you tried specifying the username with a valid UPN username? See my updated answer. – RvdV79 Jan 21 '15 at 11:39

2 Answers2

1

Do you require networking credentials (username, password) to access that particular server? If so than you might want to have a look at setting principal policies, example:

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity idnt = new WindowsIdentity(username, negotiation_type);
WindowsImpersonationContext context = idnt.Impersonate();
File.Copy(@"\\192.100.0.2\temp", @"D:\WorkDir\TempDir\test.txt", true);
context.Undo();

Note that you will have to provide a valid username (see this MSDN reference for more information). The username will have to be in the format of a UPN (User Principal Name, formatted in an e-mailaddress like format) which is usually denoted as: john_doe@example.com It will require the Internet domain.

The negotiation type that you provide will be used to handle the authentication (AD). More information can be found here

Edit:
If this approach isn't helpful (for instance when your server isn't connected to your domain) and not working for you, you might want to consider using an FTP client. File.Copy does not support URI formatted strings, so this SO Question might help as well then.

Community
  • 1
  • 1
RvdV79
  • 2,002
  • 16
  • 36
  • I tried that and getting this error providing a user name is not properly formed account name – enderaric Jan 20 '15 at 15:00
  • Can you update your question with that code? Don't forget to remove your credentials ;-) – RvdV79 Jan 20 '15 at 22:23
  • WindowsIdentity idnt = new WindowsIdentity(username, negotiation_type); I have error with this line. I can connect this server with windows authentication. But I cannot login with the same server. – enderaric Jan 21 '15 at 13:55
  • my exact problem is connecting to server now . I don't know how to login server with c# codes – enderaric Jan 22 '15 at 14:17
  • It depends on how that server is configured, if it is located within your domain, can you access the server by means of FTP. Do you require VPN or Remote Desktop to access that server or is a share available from which you can do whatever you want? If you have answers on that one, you should update your question so that we can assist. But look at Google and other SO topics too :-) – RvdV79 Jan 22 '15 at 16:29
0

The problem was about Server permissions. So my problem was solved after getting permission.

enderaric
  • 142
  • 4
  • 15