Reputation: 162
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.
Upvotes: 0
Views: 7831
Reputation: 162
The problem was about Server permissions. So my problem was solved after getting permission.
Upvotes: 0
Reputation: 2022
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: [email protected] 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.
Upvotes: 1