您的位置:首页 > 财经 > 产业 > 进入公众号后没有什么显示_中国新闻发布_泰安网站建设_百度指数分析数据

进入公众号后没有什么显示_中国新闻发布_泰安网站建设_百度指数分析数据

2025/4/3 23:02:49 来源:https://blog.csdn.net/szy13323042191/article/details/146599466  浏览:    关键词:进入公众号后没有什么显示_中国新闻发布_泰安网站建设_百度指数分析数据
进入公众号后没有什么显示_中国新闻发布_泰安网站建设_百度指数分析数据

C#使用用户名密码连接共享文件夹

创建连接

using System.ComponentModel;
using System.Runtime.InteropServices;namespace Tests.ConsoleApp
{public class ShareDirectoryConnect : IDisposable{private static readonly HashSet<Guid> _TOKENS = new HashSet<Guid>();private readonly string _username;private readonly string _password;private readonly string _path;private readonly Guid _token = Guid.NewGuid();private readonly object _lock = new object();public Guid Guid => _token;public ShareDirectoryConnect(string path, string username, string password){_path = path;_username = username;_password = password;_token = Guid.NewGuid();lock (_lock){if (_TOKENS.Count == 0)Initalize();_TOKENS.Add(_token);}}public static ShareDirectoryConnect Connect(string path, string username, string password){return new ShareDirectoryConnect(path, username, password);}private void Initalize(){USE_INFO_2 useInfo = new USE_INFO_2{ui2_remote = _path,ui2_username = _username,ui2_password = _password,ui2_domainname = string.Empty,ui2_asg_type = 0,ui2_usecount = 1};int result = NetUseAdd(null, 2, ref useInfo, out int paramError);if (result != 0)throw new Win32Exception(result);}[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]public struct USE_INFO_2{public string ui2_local;public string ui2_remote;public string ui2_password;public int ui2_status;public int ui2_asg_type;public int ui2_refcount;public int ui2_usecount;public string ui2_username;public string ui2_domainname;}[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]private static extern int NetUseAdd(string uncServerName,int level,ref USE_INFO_2 buf,out int paramError);[DllImport("netapi32.dll", CharSet = CharSet.Unicode)]private static extern int NetUseDel(string uncServerName,string useName,int forceCond);public void Dispose(){lock (_lock){if (_TOKENS.Contains(_token))_TOKENS.Remove(_token);if (_TOKENS.Count == 0)NetUseDel(null, _path, 2);}}}
}

测试

internal class Program
{private static readonly string _username = "连接用户名";private static readonly string _password = "连接密码";private static readonly string _path = @"\\192.168.190.123\共享文件夹";static void Main(string[] args){string testDir = _path + @"\test0328";using (ShareDirectoryConnect.Connect(_path, _username, _password)){Directory.CreateDirectory(testDir);Console.WriteLine("Created directory!");}List<Task> tasks = new List<Task>();for (int i = 0; i < 20; i++){Task task = Task.Run(() =>{for (int j = 0; j < 5; j++){using (ShareDirectoryConnect.Connect(_path, _username, _password)){File.WriteAllText(testDir + "\\" + Guid.NewGuid().ToString() + ".txt", "Hello World");Console.WriteLine("Created file!");}}});tasks.Add(task);}Task.WaitAll(tasks.ToArray());}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com