ASP.NETCoreBlazor:网络共享上的图像/文件能够工作1分钟,然后停止工作。
创始人
2024-09-17 19:01:02
0

该问题是由应用程序连接到网络共享的方式引起的。为了解决这个问题,需要按以下步骤操作:

  1. 在应用程序启动时,使用 Windows 身份验证将用户连接到网络共享。
  2. 使用 UNC 路径引用共享文件或文件夹。
  3. 将共享文件或文件夹的访问权限授予应用程序所使用的用户。

以下是一个示例代码,展示了如何在 ASP.NET Core Blazor 应用程序中连接到网络共享和访问文件。

using System.IO;
using System.Security.Principal;

public class NetworkShareHelper
{
    private const string SHARE_PATH = @"\\server\share\folder\";

    public static FileStream GetFileStream(string fileName)
    {
        ConnectToShare();

        var filePath = Path.Combine(SHARE_PATH, fileName);
        return File.OpenRead(filePath);
    }

    private static void ConnectToShare()
    {
        var identity = WindowsIdentity.GetCurrent();
        var userName = identity.Name.Substring(identity.Name.IndexOf('\\') + 1);

        var shareUncPath = SHARE_PATH;
        var sharePath = shareUncPath.Replace(@"\\", "").Replace('/', '\\');
        var networkPath = string.Format(@"\\{0}\{1}", sharePath.Substring(0, sharePath.IndexOf("\\")), sharePath.Substring(sharePath.IndexOf("\\") + 1));

        var connectionInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = string.Format("/c net use {0} {1} /user:{2} *",
                shareUncPath, networkPath, userName),
            CreateNoWindow = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
        };

        var process = new Process();
        process.StartInfo = connectionInfo;

        process.Start();
        process.WaitForExit();

        var output = process.StandardOutput.ReadToEnd();
        var error = process.StandardError.ReadToEnd();
        var exitCode = process.ExitCode;

        if (exitCode != 0

相关内容

热门资讯

德州之星有外挂!aapoker... 德州之星有外挂!aapoker发牌逻辑,(wepoke)竟然是有挂(详细辅助AA德州教程);wpk透...
wepoke真的有挂!wpk德... wepoke真的有挂!wpk德州ai,(wepoker)好像是有挂(详细透视技巧教程)1、点击下载安...
we辅助poker德之星!wp... we辅助poker德之星!wpk机器人代打,微扑克辅助安卓版本,新版2025教程(有挂详情)1、超多...
aapoker发牌机制!线上德... aapoker发牌机制!线上德州辅助软件有用吗,(WePoke)原先存在有挂(详细辅助解密教程);科...
智星德州菠萝开挂!鱼扑克fis... 智星德州菠萝开挂!鱼扑克fishpoker俱乐部辅助,(aa扑克)都是真的是有挂(详细辅助教你攻略)...
wepoke智能ai!德州辅助... wepoke智能ai!德州辅助软件线上,(WepokE)好像是有挂(详细辅助普及教程)是一款可以让一...
wepoke ai辅助!wep... 《wepoke ai辅助!wepoke调控参数,(来玩德州app)往昔有挂(详细透视2025新版总结...
wepoke辅助德之星!wep... wepoke辅助德之星!wepower德州扑克,(AAPoKER)素来有挂(详细辅助2025新版总结...
德州AI智能辅助机器人!来玩德... 德州AI智能辅助机器人!来玩德州有外挂吗,(nzt德州)原来是真的有挂(详细透视实用技巧);建议优先...
wepoke智能ai!wepo... 1、wepoke智能ai!wepoke打伙牌,(德州扑克)起初是有挂(详细透视科技教程);详细教程。...