我写了一些代码,要求我保存文本文件.但是,我需要将它保存到我的项目根目录,这样任何人都可以访问它,而不仅仅是我.
这是有问题的方法:
private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
string fileName = Microsoft.VisualBasic.Interaction.InputBox("Please enter a save file name.", "Save Game");
if (fileName.Equals(""))
{
MessageBox.Show("Please enter a valid save file name.");
}
else
{
fileName = String.Concat(fileName, ".gls");
MessageBox.Show("Saving to " + fileName);
System.IO.File.WriteAllText(saveScene.ToString(), AppDomain.CurrentDomain.BaseDirectory + @"\" + fileName);
}
}
catch (Exception f)
{
System.Diagnostics.Debug.Write(f);
}
}
Run Code Online (Sandbox Code Playgroud)
许多人告诉我,使用AppDomain.CurrentDomain.BaseDirectory将包含应用程序存储位置的动态位置.但是,当我执行此操作时,没有任何反应,也没有创建文件.
有没有其他方法可以做到这一点,还是我只是完全错误地使用它?
我试图从Visual Studio中的资源访问和读取文本文件'achInfo.txt'.这个网站上已经列出了一些解决方法,但它们似乎都不适用于我.他们只给我两个错误中的一个,我将在后面解释.
到目前为止,这是整个方法.
private string[] GetAchMetadata(short element)
{
string[] temp = { "", "" };
string cLine;
try
{
StreamReader sr = new StreamReader(Properties.Resources.achInfo);
while (!sr.EndOfStream)
{
cLine = sr.ReadLine();
if (Microsoft.VisualBasic.Information.IsNumeric(cLine))
{
if (int.Parse(cLine) == element)
{
temp[0] = cLine;
temp[1] = sr.ReadLine();
return temp;
}
}
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("There was a problem in collecting data.");
System.Diagnostics.Debug.Write(e);
}
return temp;
}
Run Code Online (Sandbox Code Playgroud)
我的第一个假设是使用Properties.Resources.achInfo,因为这直接指向相关文件.但是,这会引发System.ArgumentException错误,描述为"路径中的非法字符".
然后我使用了汇编解决方案('Grandma_Lair'是我的命名空间,不要问.'):
Assembly asm;
asm = Assembly.GetExecutingAssembly();
StreamReader sr = …Run Code Online (Sandbox Code Playgroud)