我该如何把 string filePath = @"\\111.11.111.11\共享\error_station\error_stations.txt"; NetworkCredential myCreds = new NetworkCredential("use", "pass"); CredentialCache myCredentialCache = new CredentialCache { { new Uri(@"file://\\111.11.111.11"), "共享", myCreds } }; WebRequest.DefaultWebProxy.Credentials = myCredentialCache; 更改为 private static readonly string FILE_PATH = @"\\111.11.111.11\共享\error_station\error_stations.txt"; private static readonly string FILE_ZD = @"\111.11.111.11\共享\error_station\fileZD.txt"; private readonly Dictionary<string, int> stationErrorTypes = new Dictionary<string, int>(); private readonly StringBuilder sb = new StringBuilder(); private readonly CredentialCache _cache; public ErrorStationHandler(CredentialCache cache) { _cache = cache; stationErrorTypes = LoadDictionary(FILE_ZD); }类型的调用
时间: 2024-04-10 14:35:10 浏览: 95
Android Uri转Path
要将给定的代码片段更改为 `ErrorStationHandler` 类的调用,您可以按照以下步骤进行修改:
1. 将 `string filePath = @"\\111.11.111.11\共享\error_station\error_stations.txt";` 更改为 `private static readonly string FILE_PATH = @"\\111.11.111.11\共享\error_station\error_stations.txt";`。这将声明一个私有的只读字段 `FILE_PATH`,并将其值设置为指定的文件路径。
2. 将 `NetworkCredential myCreds = new NetworkCredential("use", "pass");` 更改为 `CredentialCache myCreds = cache;`。这将使用传入的 `cache` 参数来设置凭据缓存。
3. 将 `CredentialCache myCredentialCache = new CredentialCache { { new Uri(@"file://\\111.11.111.11"), "共享", myCreds } };` 更改为 `_cache = cache;`。这将使用传入的 `cache` 参数来设置 `_cache` 字段。
4. 将 `WebRequest.DefaultWebProxy.Credentials = myCredentialCache;` 移除,因为在这种情况下它不再需要。
最终代码如下所示:
```csharp
private static readonly string FILE_PATH = @"\\111.11.111.11\共享\error_station\error_stations.txt";
private static readonly string FILE_ZD = @"\111.11.111.11\共享\error_station\fileZD.txt";
private readonly Dictionary<string, int> stationErrorTypes = new Dictionary<string, int>();
private readonly StringBuilder sb = new StringBuilder();
private readonly CredentialCache _cache;
public ErrorStationHandler(CredentialCache cache)
{
_cache = cache;
stationErrorTypes = LoadDictionary(FILE_ZD);
}
```
通过将代码片段放入 `ErrorStationHandler` 类的构造函数中,您可以在创建 `ErrorStationHandler` 实例时进行调用。请注意,`CredentialCache` 参数需要在实例化 `ErrorStationHandler` 时传递给构造函数。
阅读全文