forked from wakatime/visualstudio-wakatime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigFileHelper.cs
58 lines (50 loc) · 1.89 KB
/
ConfigFileHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace WakaTime.WakaTime {
class ConfigFileHelper {
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
/// <summary>
/// Retrive ApiKey from config file.
/// </summary>
/// <returns></returns>
public static string getApiKey()
{
StringBuilder keyValue = new StringBuilder(255);
string configFilepath = getConfigFilePath();
if (string.IsNullOrWhiteSpace(configFilepath) == false)
{
if (GetPrivateProfileString("settings", "api_key", "", keyValue, 255, configFilepath) > 0)
{
return keyValue.ToString();
}
}
return null;
}
/// <summary>
/// Update ApiKey file in config file
/// </summary>
/// <returns></returns>
public static void updateApiKey(string apiKey)
{
string configFilepath = getConfigFilePath();
if (string.IsNullOrWhiteSpace(apiKey) == false)
{
WritePrivateProfileString("settings", "api_key", apiKey, configFilepath);
}
}
public static string getConfigFilePath()
{
string cfgFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
if (string.IsNullOrWhiteSpace(cfgFilePath) == false)
{
string cfgfileName = cfgFilePath + "\\.wakatime.cfg";
return cfgfileName;
}
return null;
}
}
}