代码之家  ›  专栏  ›  技术社区  ›  Agent_Spock

C Win窗体-仅允许在本地PC上保存文件

  •  0
  • Agent_Spock  · 技术社区  · 7 年前

    我正在尝试通过我的软件备份SQL数据库。运行时,软件会询问用户希望将备份存储在哪里。

    现在,我的要求是用户只能在PC上创建备份,或者在连接到PC的任何PenDrive上创建备份,而不能在网络PC上创建备份。

    这是我使用的代码:

    SaveFileDialog obj = new SaveFileDialog();
    obj.DefaultExt = ".bak";
    obj.FileName = FileName;
    obj.ShowDialog();
    if (obj.FileName != null && obj.FileName != "" && obj.FileName.StartsWith("\\\\") == false)
    {
         //Save File Work
    }
    

    obj.FileName.StartsWith("\\\\") == false

    1 回复  |  直到 7 年前
        1
  •  0
  •   Pranay Rana    7 年前

    new Uri(mypath).IsUnc

    if (new Uri(path).IsUnc || IsNetworkPath(path))
    {
      //show message not allowed
      return;
    }
    
    //for checking path is network or not 
    private bool IsNetworkPath(path) 
    {
        FileInfo file = new FileInfo(path);
        DriveInfo drive = new DriveInfo(file.Directory.Root.FullName);
        return dive.DrivType == DrivType .Network;    
    }