SharpCifs.Smb.SmbFile.Exists C# (CSharp) Method

Exists() public method

Tests to see if the SMB resource exists.
Tests to see if the SMB resource exists. If the resource refers only to a server, this method determines if the server exists on the network and is advertising SMB services. If this resource refers to a workgroup, this method determines if the workgroup name is valid on the local SMB network. If this SmbFile refers to the root smb:// resource true is always returned. If this SmbFile is a traditional file or directory, it will be queried for on the specified server as expected.
public Exists ( ) : bool
return bool
        public virtual bool Exists()
        {
            if (_attrExpiration > Runtime.CurrentTimeMillis())
            {
                return _isExists;
            }
            _attributes = AttrReadonly | AttrDirectory;
            _createTime = 0L;
            _lastModified = 0L;
            _isExists = false;
            try
            {
                if (Url.GetHost().Length == 0)
                {
                }
                else
                {
                    if (_share == null)
                    {
                        if (GetType() == TypeWorkgroup)
                        {
                            UniAddress.GetByName(Url.GetHost(), true);
                        }
                        else
                        {
                            UniAddress.GetByName(Url.GetHost()).GetHostName();
                        }
                    }
                    else
                    {
                        if (GetUncPath0().Length == 1 || Runtime.EqualsIgnoreCase(_share, "IPC$"))
                        {
                            Connect0();
                        }
                        else
                        {
                            // treeConnect is good enough
                            IInfo info = QueryPath(GetUncPath0(), Trans2QueryPathInformationResponse.SMB_QUERY_FILE_BASIC_INFO
                                );
                            _attributes = info.GetAttributes();
                            _createTime = info.GetCreateTime();
                            _lastModified = info.GetLastWriteTime();
                        }
                    }
                }
                _isExists = true;
            }
            catch (UnknownHostException)
            {
            }
            catch (SmbException se)
            {
                switch (se.GetNtStatus())
                {
                    case NtStatus.NtStatusNoSuchFile:
                    case NtStatus.NtStatusObjectNameInvalid:
                    case NtStatus.NtStatusObjectNameNotFound:
                    case NtStatus.NtStatusObjectPathNotFound:
                        {
                            break;
                        }

                    default:
                        {
                            throw;
                        }
                }
            }
            _attrExpiration = Runtime.CurrentTimeMillis() + AttrExpirationPeriod;
            return _isExists;
        }

Usage Example

Example #1
0
 /// <summary>
 /// Creates a directory with the path specified by this <tt>SmbFile</tt>
 /// and any parent directories that do not exist.
 /// </summary>
 /// <remarks>
 /// Creates a directory with the path specified by this <tt>SmbFile</tt>
 /// and any parent directories that do not exist. This method will fail
 /// when used with <code>smb://</code>, <code>smb://workgroup/</code>,
 /// <code>smb://server/</code>, or <code>smb://server/share/</code> URLs
 /// because workgroups, servers, and shares cannot be dynamically created
 /// (although in the future it may be possible to create shares).
 /// </remarks>
 /// <exception cref="SmbException">SmbException</exception>
 /// <exception cref="SharpCifs.Smb.SmbException"></exception>
 public virtual void Mkdirs()
 {
     SmbFile parent;
     try
     {
         parent = new SmbFile(GetParent(), Auth);
     }
     catch (IOException)
     {
         return;
     }
     if (parent.Exists() == false)
     {
         parent.Mkdirs();
     }
     Mkdir();
 }