在我的 C# 项目中有 APIKeys.cs 文件,其中包含带有 API key 的常量字符串。 我希望这些字符串在 Git 服务器中为空,但在我的本地计算机中有实际的 API key 。 所以 pull 项目的人可以毫无问题地编译它,而且我的本地计算机仍然会在同一个文件中有 API key 。
如果我尝试上传带有空字符串的 APIKeys.cs 文件,那么我无法拥有带有 API key 的本地文件,因为当我尝试推送它时,它会覆盖空的 APIKeys.cs 文件。另外我也不能忽略这个文件,因为它会从 Git 服务器中删除空的 APIKeys.cs 文件。
那么什么是解决此问题的最佳自动化方法,它允许服务器中的类文件包含空字符串,因此当人们 pull 它并在本地计算机中拥有真实的类文件时,项目将是可编译的?
最佳答案
我现在想到了另一个解决方案,它并不完美,但对我来说仍然足够好,例如:
APIKeys.cs
文件:
public static partial class APIKeys
{
public static readonly string ImgurClientID = "";
public static readonly string ImgurClientSecret = "";
public static readonly string GoogleClientID = "";
public static readonly string GoogleClientSecret = "";
public static readonly string PastebinKey = "";
...
}
APIKeysLocal.cs
文件:
public static partial class APIKeys
{
static APIKeys()
{
ImgurClientID = "1234567890";
ImgurClientSecret = "1234567890";
GoogleClientID = "1234567890";
GoogleClientSecret = "1234567890";
PastebinKey = "1234567890";
...
}
}
忽略 Git 中的 APIKeysLocal.cs
文件,没有此文件的人如果从解决方案资源管理器中删除此文件,仍然可以编译项目。
如果使用项目预构建事件,我还会自动创建空的 APIKeysLocal.cs
文件:
cd $(ProjectDir)APIKeys\
if not exist APIKeysLocal.cs (
type nul > APIKeysLocal.cs
)
这样用户不需要做任何事情就可以编译项目。
关于c# - 在 Git 中 stash API key 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21774844/