### Get Disk Space Information with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Retrieve total capacity, free space, and used space for SMB shares. This example demonstrates how to format the output in GB. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile share = new SmbFile("smb://server/share/", ctx)) { // Total capacity (for TYPE_SHARE) long total = share.length(); // Free space long free = share.getDiskFreeSpace(); // Calculate used space long used = total - free; System.out.printf("Total: %.2f GB%n", total / 1073741824.0); System.out.printf("Free: %.2f GB%n", free / 1073741824.0); System.out.printf("Used: %.2f GB (%.1f%%)%n", used / 1073741824.0, (used * 100.0) / total); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Build jcifs-ng from Source with Maven Source: https://github.com/agno3/jcifs-ng/blob/master/README.md Run this command to install the newest master version of jcifs-ng into your local Maven repository. It cleans, installs, skips tests, and skips Javadoc and GPG signing. ```bash mvn -C clean install -DskipTests -Dmaven.javadoc.skip=true -Dgpg.skip=true ``` -------------------------------- ### Read File Content from SMB Share Source: https://context7.com/agno3/jcifs-ng/llms.txt Read the content of a file from an SMB share using `SmbFile`, `SmbFileInputStream`, and `BufferedReader`. This example demonstrates reading line by line. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); props.setProperty("jcifs.smb.client.maxVersion", "SMB311"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://fileserver/shared/data.txt", ctx); SmbFileInputStream fis = file.openInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(fis))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Get File Security and ACLs with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Retrieve owner SIDs, group SIDs, file Access Control Entries (ACEs), and share ACEs for a given file. Ensure proper authentication and context setup. ```java import jcifs.CIFSContext; import jcifs.ACE; import jcifs.SID; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "admin", "pass")); try (SmbFile file = new SmbFile("smb://server/share/document.txt", ctx)) { // Get owner SID ownerUser = file.getOwnerUser(); if (ownerUser != null) { System.out.println("Owner: " + ownerUser.toDisplayString()); System.out.println("Owner SID: " + ownerUser.toString()); } // Get owner group SID ownerGroup = file.getOwnerGroup(); if (ownerGroup != null) { System.out.println("Group: " + ownerGroup.toDisplayString()); } // Get file ACEs (DACL) ACE[] aces = file.getSecurity(true); // resolveSids=true if (aces != null) { System.out.println("\n=== File Permissions ==="); for (ACE ace : aces) { System.out.printf("%s: %s (0x%08X) - %s%n", ace.getSID().toDisplayString(), ace.isAllow() ? "ALLOW" : "DENY", ace.getAccessMask(), ace.getApplyToText()); } } // Get share ACEs ACE[] shareAces = file.getShareSecurity(true); if (shareAces != null) { System.out.println("\n=== Share Permissions ==="); for (ACE ace : shareAces) { System.out.printf("%s: %s (0x%08X)%n", ace.getSID().toDisplayString(), ace.isAllow() ? "ALLOW" : "DENY", ace.getAccessMask()); } } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Get File Attributes and Timestamps Source: https://context7.com/agno3/jcifs-ng/llms.txt Use this snippet to retrieve basic file information such as name, path, size, type, and various timestamps. It also shows how to check for read-only, hidden, and system attributes. ```java import jcifs.CIFSContext; import jcifs.SmbConstants; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Date; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://server/share/document.txt", ctx)) { if (file.exists()) { // Basic info System.out.println("Name: " + file.getName()); System.out.println("Path: " + file.getPath()); System.out.println("Size: " + file.length() + " bytes"); System.out.println("Is Directory: " + file.isDirectory()); System.out.println("Is File: " + file.isFile()); System.out.println("Is Hidden: " + file.isHidden()); System.out.println("Can Read: " + file.canRead()); System.out.println("Can Write: " + file.canWrite()); // Timestamps System.out.println("Created: " + new Date(file.createTime())); System.out.println("Modified: " + new Date(file.lastModified())); System.out.println("Accessed: " + new Date(file.lastAccess())); // Raw attributes int attrs = file.getAttributes(); System.out.println("Read-only: " + ((attrs & SmbConstants.ATTR_READONLY) != 0)); System.out.println("System: " + ((attrs & SmbConstants.ATTR_SYSTEM) != 0)); System.out.println("Archive: " + ((attrs & SmbConstants.ATTR_ARCHIVE) != 0)); } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Random Access File Operations with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Utilizes `SmbRandomAccessFile` for reading and writing data at specific positions within a file, similar to `java.io.RandomAccessFile`. Supports operations like seeking, writing integers and UTF strings, getting file length, and truncating. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.smb.SmbRandomAccessFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://server/share/data.bin", ctx); SmbRandomAccessFile raf = file.openRandomAccess("rw")) { // "r" for read-only // Write at beginning raf.writeInt(12345); raf.writeUTF("Hello"); // Seek to position and read raf.seek(0); int value = raf.readInt(); String text = raf.readUTF(); System.out.println("Read: " + value + ", " + text); // Get file length and position System.out.println("File length: " + raf.length()); System.out.println("Current position: " + raf.getFilePointer()); // Truncate file raf.setLength(100); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Create and Delete Files and Directories with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Demonstrates creating directories (including parent directories), creating new files, deleting files, and recursively deleting directories. Ensure proper error handling for file operations. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); // Create new directory (and parent directories if needed) try (SmbFile dir = new SmbFile("smb://server/share/newdir/subdir/", ctx)) { dir.mkdirs(); System.out.println("Directory created: " + dir.exists()); } // Create new file (fails if file already exists) try (SmbFile file = new SmbFile("smb://server/share/newdir/newfile.txt", ctx)) { file.createNewFile(); System.out.println("File created: " + file.exists()); } // Delete file try (SmbFile file = new SmbFile("smb://server/share/newdir/newfile.txt", ctx)) { file.delete(); System.out.println("File deleted: " + !file.exists()); } // Delete directory (recursively deletes contents) try (SmbFile dir = new SmbFile("smb://server/share/newdir/", ctx)) { dir.delete(); System.out.println("Directory deleted: " + !dir.exists()); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Use Singleton Context for SMB Operations Source: https://context7.com/agno3/jcifs-ng/llms.txt Register the SMB URL handler and obtain the singleton context for use with `SmbFile`. This context is initialized from system properties and an optional properties file. ```java import jcifs.context.SingletonContext; import jcifs.smb.SmbFile; // Register SMB URL handler (required for java.net.URL usage) SingletonContext.registerSmbURLHandler(); // Get singleton instance (initialized on first call) SingletonContext ctx = SingletonContext.getInstance(); // Create SmbFile using singleton context try (SmbFile file = new SmbFile("smb://server/share/document.txt", ctx)) { if (file.exists()) { System.out.println("Size: " + file.length() + " bytes"); System.out.println("Last modified: " + new java.util.Date(file.lastModified())); } } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Handle SMB Exceptions with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Demonstrates how to handle various SMB exceptions, including authentication failures and specific SMB errors like file not found, access denied, sharing violations, and disk full. Uses a switch statement to map NT Status codes to human-readable messages. ```java import jcifs.CIFSContext; import jcifs.CIFSException; import jcifs.smb.SmbFile; import jcifs.smb.SmbException; import jcifs.smb.SmbAuthException; import jcifs.smb.NtStatus; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://server/share/file.txt", ctx)) { // Attempt operations... byte[] data = new byte[1024]; try (var is = file.openInputStream()) { is.read(data); } } catch (SmbAuthException e) { // Authentication failed System.err.println("Authentication failed: " + e.getMessage()); System.err.println("NT Status: 0x" + Integer.toHexString(e.getNtStatus())); } catch (SmbException e) { // Handle specific SMB errors int status = e.getNtStatus(); switch (status) { case NtStatus.NT_STATUS_OBJECT_NAME_NOT_FOUND: case NtStatus.NT_STATUS_OBJECT_PATH_NOT_FOUND: System.err.println("File or path not found"); break; case NtStatus.NT_STATUS_ACCESS_DENIED: System.err.println("Access denied"); break; case NtStatus.NT_STATUS_SHARING_VIOLATION: System.err.println("File is in use by another process"); break; case NtStatus.NT_STATUS_DISK_FULL: System.err.println("Disk is full"); break; default: System.err.println("SMB error: " + e.getMessage()); System.err.println("NT Status: 0x" + Integer.toHexString(status)); } } ctx.close(); } catch (CIFSException e) { System.err.println("CIFS configuration error: " + e.getMessage()); } ``` -------------------------------- ### Configure SMB Protocol Versions Source: https://github.com/agno3/jcifs-ng/blob/master/README.md Control the negotiated SMB protocol levels using system properties. This allows specifying the minimum and maximum versions supported by the client. ```properties jcifs.smb.client.minVersion=SMB1 jcifs.smb.client.maxVersion=SMB210 ``` -------------------------------- ### Using SingletonContext for Backward Compatibility Source: https://github.com/agno3/jcifs-ng/blob/master/README.md To maintain the previous behavior of global shared state and system property configuration, pass jcifs.context.SingletonContext.getInstance() for context parameters. Replace NtlmPasswordAuthentication parameters with this pattern. ```java SingletonContext.getInstance().withCredentials(ntlmPasswordAuthentication) ``` -------------------------------- ### Copy and Rename Files with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Shows how to copy files and entire directories recursively between locations, and how to rename files. The `copyTo` method uses concurrent threads for efficiency. Renaming is restricted to the same share. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); // Copy file to new location try (SmbFile source = new SmbFile("smb://server/share/original.txt", ctx); SmbFile dest = new SmbFile("smb://server/share/backup/copy.txt", ctx)) { source.copyTo(dest); System.out.println("Copied: " + dest.exists()); } // Copy entire directory recursively try (SmbFile sourceDir = new SmbFile("smb://server/share/docs/", ctx); SmbFile destDir = new SmbFile("smb://server/share/docs_backup/", ctx)) { sourceDir.copyTo(destDir); } // Rename file (same share only) try (SmbFile file = new SmbFile("smb://server/share/oldname.txt", ctx); SmbFile newFile = new SmbFile("smb://server/share/newname.txt", ctx)) { file.renameTo(newFile); // Note: file object still refers to old name } // Rename with replace (SMB2+ only) try (SmbFile file = new SmbFile("smb://server/share/source.txt", ctx); SmbFile target = new SmbFile("smb://server/share/target.txt", ctx)) { file.renameTo(target, true); // replace=true overwrites existing } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Communicate via Named Pipes with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Demonstrates using SmbNamedPipe for both transaction and byte-mode operations. Ensure proper context and credentials are set up. ```java import jcifs.CIFSContext; import jcifs.SmbPipeHandle; import jcifs.SmbPipeResource; import jcifs.smb.SmbNamedPipe; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); // Transact mode - write and read in single transaction try (SmbNamedPipe pipe = new SmbNamedPipe( "smb://server/IPC$/PIPE/mypipe", SmbPipeResource.PIPE_TYPE_RDWR | SmbPipeResource.PIPE_TYPE_TRANSACT, ctx); SmbPipeHandle handle = pipe.openPipe()) { try (OutputStream out = handle.getOutput(); InputStream in = handle.getInput()) { // Write request byte[] request = "REQUEST".getBytes(); out.write(request); // Read response byte[] response = new byte[1024]; int len = in.read(response); System.out.println("Response: " + new String(response, 0, len)); } } // Byte mode - standard read/write operations try (SmbNamedPipe pipe = new SmbNamedPipe( "smb://server/IPC$/PIPE/bytepipe", SmbPipeResource.PIPE_TYPE_RDWR, ctx); SmbPipeHandle handle = pipe.openPipe()) { try (OutputStream out = handle.getOutput(); InputStream in = handle.getInput()) { out.write("HELLO".getBytes()); out.flush(); byte[] buffer = new byte[256]; int read = in.read(buffer); System.out.println("Received: " + new String(buffer, 0, read)); } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Write and Append to SMB File Source: https://context7.com/agno3/jcifs-ng/llms.txt Use SmbFileOutputStream to write data to SMB files. Supports append mode. Ensure proper context and credentials are set up. ```java import jcifs.CIFSContext; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileOutputStream; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://server/share/output.txt", ctx); SmbFileOutputStream fos = file.openOutputStream(); PrintWriter writer = new PrintWriter( new OutputStreamWriter(fos, StandardCharsets.UTF_8))) { writer.println("Line 1: Hello from jcifs-ng"); writer.println("Line 2: Written at " + new java.util.Date()); } // Append to existing file try (SmbFile file = new SmbFile("smb://server/share/output.txt", ctx); SmbFileOutputStream fos = file.openOutputStream(true)) { // append=true fos.write("Appended data\n".getBytes(StandardCharsets.UTF_8)); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Enable SMB2 Support (jcifs-ng 2.0) Source: https://github.com/agno3/jcifs-ng/blob/master/README.md Configure SMB2 support in jcifs-ng 2.0 by setting the 'jcifs.smb.client.enableSMB2' property. This may also be chosen if the server does not support SMB1 dialects. ```properties jcifs.smb.client.enableSMB2 ``` -------------------------------- ### Create Authenticated CIFS Context Source: https://context7.com/agno3/jcifs-ng/llms.txt Create a `CIFSContext` with custom properties and NTLM authentication. Ensure the context is closed when no longer needed. ```java import jcifs.CIFSContext; import jcifs.CIFSException; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; // Create configuration with custom properties Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); props.setProperty("jcifs.smb.client.maxVersion", "SMB311"); props.setProperty("jcifs.smb.client.responseTimeout", "30000"); props.setProperty("jcifs.smb.client.soTimeout", "35000"); try { // Create base context with configuration CIFSContext baseContext = new BaseContext(new PropertyConfiguration(props)); // Create authenticated context with credentials NtlmPasswordAuthenticator auth = new NtlmPasswordAuthenticator( "DOMAIN", // domain "username", // username "password" // password ); CIFSContext authenticatedContext = baseContext.withCredentials(auth); // Use the context to access resources try (var resource = authenticatedContext.get("smb://server/share/file.txt")) { System.out.println("File exists: " + resource.exists()); } // Always close context when done baseContext.close(); } catch (CIFSException e) { e.printStackTrace(); } ``` -------------------------------- ### List Directory Contents with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Iterate over directory contents using `children()` for streaming access or `listFiles()` for array-based access. Supports wildcard filtering. Ensure proper context and credentials are set up. ```java import jcifs.CIFSContext; import jcifs.CloseableIterator; import jcifs.SmbResource; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile dir = new SmbFile("smb://server/share/documents/", ctx)) { // Method 1: Using streaming iterator (memory efficient) System.out.println("=== All files ==="); try (CloseableIterator iter = dir.children()) { while (iter.hasNext()) { try (SmbResource res = iter.next()) { String type = res.isDirectory() ? "[DIR]" : "[FILE]"; System.out.printf("%s %s (%d bytes)%n", type, res.getName(), res.length()); } } } // Method 2: Using wildcard pattern (server-side filtering) System.out.println("=== PDF files only ==="); try (CloseableIterator iter = dir.children("*.pdf")) { while (iter.hasNext()) { try (SmbResource res = iter.next()) { System.out.println(res.getName()); } } } // Method 3: Array-based listing SmbFile[] files = dir.listFiles(); System.out.println("Total items: " + files.length); for (SmbFile f : files) { f.close(); // Important when using strict resource lifecycle } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Configure jcifs-ng Properties Source: https://context7.com/agno3/jcifs-ng/llms.txt Set various configuration properties for jcifs-ng, including protocol versions, timeouts, credentials, security settings, buffer sizes, DFS, name resolution, and resource lifecycle management. Instantiate a BaseContext with these properties. ```java import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import java.util.Properties; Properties props = new Properties(); // === Protocol Version === props.setProperty("jcifs.smb.client.minVersion", "SMB202"); // SMB1, SMB202, SMB210, SMB300, SMB302, SMB311 props.setProperty("jcifs.smb.client.maxVersion", "SMB311"); // === Timeouts (milliseconds) === props.setProperty("jcifs.smb.client.responseTimeout", "30000"); // SMB response timeout props.setProperty("jcifs.smb.client.soTimeout", "35000"); // Socket timeout props.setProperty("jcifs.smb.client.connTimeout", "35000"); // Connection timeout props.setProperty("jcifs.smb.client.sessionTimeout", "35000"); // Session timeout // === Default Credentials === props.setProperty("jcifs.smb.client.domain", "WORKGROUP"); props.setProperty("jcifs.smb.client.username", "user"); props.setProperty("jcifs.smb.client.password", "pass"); // === Security Settings === props.setProperty("jcifs.smb.client.signingPreferred", "true"); // Enable signing props.setProperty("jcifs.smb.client.signingEnforced", "false"); // Require signing props.setProperty("jcifs.smb.client.encryptionEnabled", "false"); // SMB3 encryption props.setProperty("jcifs.smb.lmCompatibility", "3"); // NTLMv2 only (3-5) // === Buffer Sizes === props.setProperty("jcifs.smb.client.rcv_buf_size", "65535"); props.setProperty("jcifs.smb.client.snd_buf_size", "65535"); // === DFS Settings === props.setProperty("jcifs.smb.client.dfs.disabled", "false"); props.setProperty("jcifs.smb.client.dfs.ttl", "300"); // Cache TTL in seconds // === Name Resolution === props.setProperty("jcifs.resolveOrder", "DNS,BCAST"); // DNS,BCAST,WINS,LMHOSTS props.setProperty("jcifs.netbios.wins", "192.168.1.1"); // === Resource Lifecycle === props.setProperty("jcifs.smb.client.strictResourceLifecycle", "false"); // Create context with these properties try { var ctx = new BaseContext(new PropertyConfiguration(props)); // Use context... ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Watch Directory Changes with jcifs-ng Source: https://context7.com/agno3/jcifs-ng/llms.txt Monitors a specified directory for file system events like creation, deletion, modification, and renames. Supports recursive watching. ```java import jcifs.CIFSContext; import jcifs.FileNotifyInformation; import jcifs.SmbWatchHandle; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.List; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile dir = new SmbFile("smb://server/share/watched/", ctx)) { // Create watch with filter for file changes int filter = FileNotifyInformation.FILE_NOTIFY_CHANGE_FILE_NAME | FileNotifyInformation.FILE_NOTIFY_CHANGE_DIR_NAME | FileNotifyInformation.FILE_NOTIFY_CHANGE_SIZE | FileNotifyInformation.FILE_NOTIFY_CHANGE_LAST_WRITE; try (SmbWatchHandle watch = dir.watch(filter, true)) { // recursive=true System.out.println("Watching directory for changes (Ctrl+C to stop)..."); while (true) { // Blocks until changes occur List changes = watch.watch(); for (FileNotifyInformation info : changes) { String action = switch (info.getAction()) { case FileNotifyInformation.FILE_ACTION_ADDED -> "ADDED"; case FileNotifyInformation.FILE_ACTION_REMOVED -> "REMOVED"; case FileNotifyInformation.FILE_ACTION_MODIFIED -> "MODIFIED"; case FileNotifyInformation.FILE_ACTION_RENAMED_OLD_NAME -> "RENAMED_FROM"; case FileNotifyInformation.FILE_ACTION_RENAMED_NEW_NAME -> "RENAMED_TO"; default -> "UNKNOWN"; }; System.out.printf("[%s] %s%n", action, info.getFileName()); } } } } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Set File Attributes and Timestamps Source: https://context7.com/agno3/jcifs-ng/llms.txt This snippet demonstrates how to modify file timestamps (creation, modification, access) and attributes, including setting read-only status and custom attributes like hidden. ```java import jcifs.CIFSContext; import jcifs.SmbConstants; import jcifs.smb.SmbFile; import jcifs.config.PropertyConfiguration; import jcifs.context.BaseContext; import jcifs.smb.NtlmPasswordAuthenticator; import java.util.Properties; Properties props = new Properties(); props.setProperty("jcifs.smb.client.minVersion", "SMB202"); try { CIFSContext ctx = new BaseContext(new PropertyConfiguration(props)) .withCredentials(new NtlmPasswordAuthenticator("DOMAIN", "user", "pass")); try (SmbFile file = new SmbFile("smb://server/share/document.txt", ctx)) { // Set individual timestamps long now = System.currentTimeMillis(); file.setLastModified(now); file.setLastAccess(now); file.setCreateTime(now - 86400000); // 1 day ago // Set all timestamps at once file.setFileTimes( now - 86400000, // createTime now, // lastModified now // lastAccess ); // Set read-only file.setReadOnly(); System.out.println("Read-only: " + !file.canWrite()); // Remove read-only file.setReadWrite(); System.out.println("Writable: " + file.canWrite()); // Set custom attributes int attrs = file.getAttributes(); attrs |= SmbConstants.ATTR_HIDDEN; file.setAttributes(attrs); System.out.println("Hidden: " + file.isHidden()); } ctx.close(); } catch (Exception e) { e.printStackTrace(); } ``` -------------------------------- ### Add jcifs-ng Dependency Source: https://github.com/agno3/jcifs-ng/blob/master/README.md Include this Maven dependency in your project to use the jcifs-ng library. Ensure you use the latest stable version. ```xml eu.agno3.jcifs jcifs-ng 2.1.9 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.