Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 440097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:50:19+00:00 2026-05-12T20:50:19+00:00

I need to write a file to a windows share on a computer not

  • 0

I need to write a file to a windows share on a computer not part of a domain from ASP.net.

I’ve tried implementing impersonation like : http://support.microsoft.com/?id=306158 which wraps the LogonUserA api call. Something isn’t working though, and I don’t know why. (It doesn’t seem to give any error code or reason message…)

I think it’s because the remote computer is NOT on a domain. ? I don’t know.

I can’t use the “matching local user/pass on both machines” trick, can someone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T20:50:19+00:00Added an answer on May 12, 2026 at 8:50 pm

    From my answer on this question, give this a try:

     using (new NetworkConnection(@"\\server\read", readCredentials))
     using (new NetworkConnection(@"\\server2\write", writeCredentials)) {
        File.Copy(@"\\server\read\file", @"\\server2\write\file");
     }
    

    I liked Mark Brackett‘s answer so much that I did my own quick implementation. Here it is if anyone else needs it in a hurry:

    public class NetworkConnection : IDisposable
    {
        string _networkName;
    
        public NetworkConnection(string networkName, 
            NetworkCredential credentials)
        {
            _networkName = networkName;
    
            var netResource = new NetResource()
            {
                Scope = ResourceScope.GlobalNetwork,
                ResourceType = ResourceType.Disk,
                DisplayType = ResourceDisplaytype.Share,
                RemoteName = networkName
            };
    
            var result = WNetAddConnection2(
                netResource, 
                credentials.Password,
                credentials.UserName,
                0);
    
            if (result != 0)
            {
                throw new Win32Exception(result, "Error connecting to remote share");
            }   
        }
    
        ~NetworkConnection()
        {
            Dispose(false);
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        protected virtual void Dispose(bool disposing)
        {
            WNetCancelConnection2(_networkName, 0, true);
        }
    
        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2(NetResource netResource, 
            string password, string username, int flags);
    
        [DllImport("mpr.dll")]
        private static extern int WNetCancelConnection2(string name, int flags,
            bool force);
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public class NetResource
    {
        public ResourceScope Scope;
        public ResourceType ResourceType;
        public ResourceDisplaytype DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }
    
    public enum ResourceScope : int
    {
        Connected = 1,
        GlobalNetwork,
        Remembered,
        Recent,
        Context
    };
    
    public enum ResourceType : int
    {
        Any = 0,
        Disk = 1,
        Print = 2,
        Reserved = 8,
    }
    
    public enum ResourceDisplaytype : int
    {
        Generic = 0x0,
        Domain = 0x01,
        Server = 0x02,
        Share = 0x03,
        File = 0x04,
        Group = 0x05,
        Network = 0x06,
        Root = 0x07,
        Shareadmin = 0x08,
        Directory = 0x09,
        Tree = 0x0a,
        Ndscontainer = 0x0b
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file and from time to time I need to write
I need to write a rule to redirect any image file to a specific
I need to write an error log to a XML file using Visual Basic
I need to write an app that reads a config file with info on
Simply I need to write echo t${count} = $t${count} To a text file, including
Both my Rails model and controller code need to write files to the file
I'm working with Quickbook's IIF file format and I need to write a parser
I need to have a newline every time I write to a file in
Do I need to malloc when creating a file to write to? The file
I'm trying to upload a file via URLConnection, but I need to read/write it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.