# Quick Reference: Utils Shared Utilities

## StdUtil Class (File System & Encoding)

### Time
```csharp
ulong ms = StdUtil.NowMsec();              // Current time in milliseconds
string time = StdUtil.Ftimed(path);         // Last write time of file
```

### File Operations
```csharp
string text = StdUtil.SafeRead(path);               // Read file and delete it
byte[] data = StdUtil.SafeReadBin(path);            // Read binary file and delete it
string text = StdUtil.SafeRetainIn(path);           // Read file without deleting
StdUtil.SafeWriteFsys(path, ext, content);          // Atomic file write
```

### Directory Operations
```csharp
string next = StdUtil.GetNextIn(dir, "*.txt");      // Get first file matching pattern
```

### Encoding/Decoding
```csharp
string hex = StdUtil.BinHexA(bytes);                // Bytes to hex string
string hex = StdUtil.asc2hex(text);                 // String to hex
string text = StdUtil.hex2asc(hex);                 // Hex to string
```

### URL Utilities
```csharp
string encoded = StdUtil.Xscape(text);              // URL encode with special handling
string decoded = StdUtil.unXscape(text);            // URL decode
string clean = StdUtil.Uncommand(url, "cmd=test");  // Remove command parameter
```

### HTML Utilities
```csharp
string body = StdUtil.BodyOf(html);                 // Extract body content
string quoted = StdUtil.Quoted(text, "root");       // Quote and escape string
```

---

## StdWeb Class (ASP.NET Web Helpers)

### Context Information
```csharp
string url = StdWeb.WinHref();                      // Current request URL
string templates = StdWeb.TemZone();                // /templates/ physical path
string work = StdWeb.WorkZone();                    // /Work/ physical path
```

### File Operations (Web Path)
```csharp
string text = StdWeb.SafeRetain(webPath);           // Read file at web path
string text = StdWeb.SafeRetainF(physicalPath);     // Read file at physical path
StdWeb.SafeWrite(webPath, ext, content);            // Atomic write at web path
StdWeb.Appender(webPath, content);                  // Append to file
StdWeb.DelWork(filename);                           // Delete file in /Work/
```

### Request Parameters
```csharp
bool has = StdWeb.HasReq("param");                  // Check if parameter exists
string val = StdWeb.SafeReq("param");               // Get parameter or empty string
string val = StdWeb.GetReq("param");                // Get parameter value
bool hasGuid = StdWeb.HasGuid("id");                // Check if parameter is valid GUID
```

### URL Validation
```csharp
bool exists = StdWeb.urlFileExists(url);            // Check if URL returns 200 OK
```

---

## Usage Examples

### In Controller Files
```csharp
using static Utils.StdUtil;
using static Utils.StdWeb;

public class MyController : ApiController
{
    public IHttpActionResult GetData()
    {
        var workPath = WorkZone();
        var timestamp = NowMsec();
        var data = SafeRetainF(workPath + "data.txt");
        return Ok(new { timestamp, data });
    }
}
```

### With Namespace
```csharp
using Utils;

public class Helper
{
    public void ProcessFile(string path)
    {
        var content = StdUtil.SafeRead(path);
        var hex = StdUtil.asc2hex(content);
        StdUtil.SafeWriteFsys(path + ".hex", ".txt", hex);
    }
}
```

---

## Common Patterns

### Atomic File Write
```csharp
// Writes to .txta, then renames to .txt (atomic operation)
StdUtil.SafeWriteFsys("/path/file", ".txt", content);
```

### Read-Once Pattern
```csharp
// Reads file and immediately deletes it (for one-time tokens, etc.)
string token = StdUtil.SafeRead("/path/token.txt");
```

### Web Request Handling
```csharp
if (StdWeb.HasReq("action") && StdWeb.HasGuid("id"))
{
    var action = StdWeb.GetReq("action");
    var id = StdWeb.GetReq("id");
    // Process...
}
