<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      WPF 使用 WNetUseConnection 連接 SMB 網(wǎng)絡(luò)資源

      為了方便起見(jiàn),我編寫(xiě)了一個(gè)簡(jiǎn)單的界面,代碼如下

          <Grid>
              <StackPanel VerticalAlignment="Center">
                  <Grid MinWidth="300" HorizontalAlignment="Center">
                      <Grid.Resources>
                          <Style TargetType="TextBlock">
                              <Setter Property="Margin" Value="0 5 5 5"></Setter>
                          </Style>
                          <Style TargetType="TextBox">
                              <Setter Property="Margin" Value="0 5 0 5"></Setter>
                          </Style>
                      </Grid.Resources>
                      <Grid.RowDefinitions>
                          <RowDefinition></RowDefinition>
                          <RowDefinition></RowDefinition>
                          <RowDefinition></RowDefinition>
                      </Grid.RowDefinitions>
                      <Grid.ColumnDefinitions>
                          <ColumnDefinition Width="Auto"></ColumnDefinition>
                          <ColumnDefinition></ColumnDefinition>
                      </Grid.ColumnDefinitions>
      
                      <TextBlock Text="Url:"></TextBlock>
                      <TextBox x:Name="UrlTextBox" Grid.Row="0" Grid.Column="1" Text="\\nas.lindexi.com\Data\"></TextBox>
      
                      <TextBlock Grid.Row="1" Grid.Column="0"  Text="賬號(hào):"></TextBlock>
                      <TextBox x:Name="UserNameTextBox" Grid.Row="1" Grid.Column="1"></TextBox>
      
                      <TextBlock Grid.Row="2" Grid.Column="0"  Text="密碼:"></TextBlock>
                      <TextBox x:Name="PasswordTextBox" Grid.Row="2" Grid.Column="1"></TextBox>
                  </Grid>
      
                  <Button x:Name="ConnectButton" Width="100" Height="30" Margin="10" Click="ConnectButton_OnClick">連接</Button>
              </StackPanel>
            
          </Grid>
      

      運(yùn)行起來(lái)的界面大概如下

      點(diǎn)擊連接按鈕時(shí),將嘗試連接以上配置的各項(xiàng)內(nèi)容

      如上圖所示,我將嘗試連接到我的 SMB 上。對(duì)于 Windows 來(lái)說(shuō),以上的 \\nas.lindexi.com\Data\ 就是一個(gè)非常合法的路徑,連接完成之后,即可直接訪(fǎng)問(wèn)

      本文的重點(diǎn)是調(diào)用 WNetUseConnection 方法進(jìn)行連接,此方法的官方說(shuō)明文檔是 WNetUseConnectionW 函數(shù) (winnetwk.h) - Win32 apps - Microsoft Learn

      其 PInvoke 定義代碼如下

          [DllImport("Mpr.dll")]
          private static extern int WNetUseConnection
          (
              IntPtr hwndOwner,
              NETRESOURCE lpNetResource,
              string lpPassword,
              string lpUserID,
              int dwFlags,
              string? lpAccessName,
              string? lpBufferSize,
              string? lpResult
          );
      

      結(jié)構(gòu)體 NETRESOURCE 的定義如下

          [StructLayout(LayoutKind.Sequential)]
          private struct NETRESOURCE()
          {
              public int dwScope = 0;
              public int dwType = 0;
              public int dwDisplayType = 0;
              public int dwUsage = 0;
              public string lpLocalName = "";
              public string lpRemoteName = "";
              public string lpComment = "";
              public string lpProvider = "";
          }
      

      為了方便調(diào)用,我編寫(xiě)了 NetworkShare 輔助類(lèi),代碼如下

      public static class NetworkShare
      {
          public static int ConnectToShare(string uri, string username, string password)
          {
              //Create netresource and point it at the share
              NETRESOURCE netResource = new NETRESOURCE();
              netResource.dwType = RESOURCETYPE_DISK;
              netResource.lpRemoteName = uri;
      
              int result = WNetUseConnection(IntPtr.Zero, netResource, password, username, 0, null, null, null);
              return result;
          }
      
          const int RESOURCETYPE_DISK = 0x00000001;
          const int CONNECT_UPDATE_PROFILE = 0x00000001;
      
          ...
      }
      

      對(duì)于 Win32 調(diào)用來(lái)說(shuō),一般都有成對(duì)的釋放代碼,這里使用的是 WNetCancelConnection 進(jìn)行斷開(kāi)

      同樣做簡(jiǎn)單的封裝

      public static class NetworkShare
      {
          public static int DisconnectFromShare(string uri, bool force)
          {
              int result = WNetCancelConnection(uri, force);
              return result;
          }
      
          [DllImport("Mpr.dll")]
          private static extern int WNetCancelConnection
          (
              string lpName,
              bool fForce
          );
      
          ...
      }
      

      整個(gè) NetworkShare 的代碼如下

      public static class NetworkShare
      {
          public static int ConnectToShare(string uri, string username, string password)
          {
              //Create netresource and point it at the share
              NETRESOURCE netResource = new NETRESOURCE();
              netResource.dwType = RESOURCETYPE_DISK;
              netResource.lpRemoteName = uri;
      
              int result = WNetUseConnection(IntPtr.Zero, netResource, password, username, 0, null, null, null);
              return result;
          }
      
          public static int DisconnectFromShare(string uri, bool force)
          {
              int result = WNetCancelConnection(uri, force);
              return result;
          }
      
          const int RESOURCETYPE_DISK = 0x00000001;
          const int CONNECT_UPDATE_PROFILE = 0x00000001;
      
          [StructLayout(LayoutKind.Sequential)]
          private struct NETRESOURCE()
          {
              public int dwScope = 0;
              public int dwType = 0;
              public int dwDisplayType = 0;
              public int dwUsage = 0;
              public string lpLocalName = "";
              public string lpRemoteName = "";
              public string lpComment = "";
              public string lpProvider = "";
          }
      
          [DllImport("Mpr.dll")]
          private static extern int WNetUseConnection
          (
              IntPtr hwndOwner,
              NETRESOURCE lpNetResource,
              string lpPassword,
              string lpUserID,
              int dwFlags,
              string? lpAccessName,
              string? lpBufferSize,
              string? lpResult
          );
      
          [DllImport("Mpr.dll")]
          private static extern int WNetCancelConnection
          (
              string lpName,
              bool fForce
          );
      }
      

      完成之后的調(diào)用代碼如下

          private void ConnectButton_OnClick(object sender, RoutedEventArgs e)
          {
              var success = NetworkShare.ConnectToShare(UrlTextBox.Text, UserNameTextBox.Text, PasswordTextBox.Text);
              if (success == 0)
              {
                  foreach (var fileSystemEntry in Directory.EnumerateFileSystemEntries(UrlTextBox.Text))
                  {
                      Debug.WriteLine(fileSystemEntry);
                  }
      
                  NetworkShare.DisconnectFromShare(UrlTextBox.Text, force: true);
              }
          }
      

      當(dāng)輸入的地址和賬號(hào)密碼正確時(shí),預(yù)期可以在 ConnectButton_OnClick 里面枚舉出當(dāng)前 SMB 網(wǎng)絡(luò)資源的各項(xiàng)文件夾和文件

      我為了方便自己調(diào)試,我還引入了 https://github.com/dotnet-campus/dotnetCampus.Configurations 硬幣配置文件庫(kù),將連接地址和賬號(hào)密碼存放在 COIN 硬幣配置文件里,其代碼如下

          public MainWindow()
          {
              InitializeComponent();
      
              var coinFile = @"C:\lindexi\Nas.coin";
              if (File.Exists(coinFile))
              {
                  var fileConfigurationRepo = ConfigurationFactory.FromFile(coinFile,RepoSyncingBehavior.Static);
                  var appConfigurator = fileConfigurationRepo.CreateAppConfigurator();
                  var nasConfiguration = appConfigurator.Of<NasConfiguration>();
                  UrlTextBox.Text = nasConfiguration.Url;
                  UserNameTextBox.Text = nasConfiguration.UserName;
                  PasswordTextBox.Text = nasConfiguration.Password;
              }
          }
      
          class NasConfiguration : Configuration
          {
              public NasConfiguration() : base("")
              {
              }
      
              public string Url => GetString();
              public string UserName => GetString();
              public string Password => GetString();
          }
      

      本文代碼放在 githubgitee 上,可以使用如下命令行拉取代碼。我整個(gè)代碼倉(cāng)庫(kù)比較龐大,使用以下命令行可以進(jìn)行部分拉取,拉取速度比較快

      先創(chuàng)建一個(gè)空文件夾,接著使用命令行 cd 命令進(jìn)入此空文件夾,在命令行里面輸入以下代碼,即可獲取到本文的代碼

      git init
      git remote add origin https://gitee.com/lindexi/lindexi_gd.git
      git pull origin 75409caab083c6ccd3337de4e8159205314e6974
      

      以上使用的是國(guó)內(nèi)的 gitee 的源,如果 gitee 不能訪(fǎng)問(wèn),請(qǐng)?zhí)鎿Q為 github 的源。請(qǐng)?jiān)诿钚欣^續(xù)輸入以下代碼,將 gitee 源換成 github 源進(jìn)行拉取代碼。如果依然拉取不到代碼,可以發(fā)郵件向我要代碼

      git remote remove origin
      git remote add origin https://github.com/lindexi/lindexi_gd.git
      git pull origin 75409caab083c6ccd3337de4e8159205314e6974
      

      獲取代碼之后,進(jìn)入 WPFDemo/NaihunojojeaKeheakabearweabe 文件夾,即可獲取到源代碼

      更多技術(shù)博客,請(qǐng)參閱 博客導(dǎo)航

      posted @ 2025-08-10 09:50  lindexi  閱讀(119)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 色综合伊人色综合网站| 久久久久综合一本久道| 大桥未久亚洲无av码在线| 亚洲精品国产精品乱码不| 亚洲 一区二区 在线| 亚洲国产综合自在线另类| 久久99热只有频精品8| 色99久久久久高潮综合影院| 精品人妻中文无码av在线| 国产欧美精品aaaaaa片| 在国产线视频A在线视频| 激情综合网激情国产av| 国产亚洲第一精品| 综合色一色综合久久网| 2021国产精品视频网站| 亚洲三区在线观看内射后入| 国产无遮挡真人免费视频| 四子王旗| 精品国产中文字幕av| 亚洲午夜香蕉久久精品| 日本久久久免费高清| 久久国内精品自在自线91| 色欲久久久天天天综合网| 一区二区三区自拍偷拍视频| 2019亚洲午夜无码天堂| 欧美一本大道香蕉综合视频| 欧美成人精品一级在线观看| 污污污污污污WWW网站免费| 久久精品夜夜夜夜夜久久| yy111111少妇无码影院| 国产一区二区三区我不卡| 18禁裸乳无遮挡自慰免费动漫 | 久久碰国产一区二区三区| 久9视频这里只有精品试看| 国产国拍亚洲精品永久软件| 午夜国产小视频| 少女韩国在线观看完整版免费| 免费久久人人爽人人爽AV| 国产成人精品亚洲精品密奴| av午夜久久蜜桃传媒软件| 欧美黑人XXXX性高清版|