我想在保存前检查隔离存储空间中的“闹钟”一词。
如果存在“报警”字样,我会将“报警”更改为“报警1”,如果“报警1”存在则更改为“报警2”。
我应该怎么做呢?
以下是我的代码但不起作用:
if (labelTextBox.Text == "")
{
try
{
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
foreach (string label in storage.GetFileNames("*"))
{
MessageBox.Show(label);
}
}
}
catch (Exception)
{
}
int i = 0;
i++;
labelTextBox.Text = "Alarm" + i;
alarmLabel = (labelTextBox.Text.ToString()).Replace(" ", "_");
}
答案 0 :(得分:0)
您可以使用 IsolatedStorageSettings.ApplicationSettings ,这更适合对象(e.r。字符串)处理。
我制作了一个小样本,使用了这个类:
using System;
using System.IO.IsolatedStorage;
using System.Windows;
using Microsoft.Phone.Controls;
namespace SidekickWP7
{
public partial class Page1 : PhoneApplicationPage
{
const string MYALARM = "MyAlarm";
public Page1()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page1_Loaded);
}
void Page1_Loaded(object sender, RoutedEventArgs e)
{
int intAlarm = 0;
Int32.TryParse(Load(MYALARM).ToString(), out intAlarm);
intAlarm++;
MessageBox.Show(intAlarm.ToString());
Save(MYALARM, intAlarm);
}
private static object Load(string strKey)
{
object objValue;
if (IsolatedStorageSettings.ApplicationSettings.TryGetValue<object>(strKey, out objValue) == false)
{
objValue = String.Empty;
}
return objValue;
}
private static void Save(string strKey, object objValue)
{
IsolatedStorageSettings.ApplicationSettings[strKey] = objValue;
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
}
答案 1 :(得分:0)
试试这个:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
int highestNumberFound = -1;
foreach (var fileName in store.GetFileNames())
{
if (fileName.StartsWith("alarm"))
{
if (fileName == "alarm")
{
if (highestNumberFound < 0)
{
highestNumberFound = 0;
}
}
else if (fileName.Length > 5)
{
int numb;
if (int.TryParse(fileName.Substring(5), out numb))
{
if (numb > highestNumberFound)
{
highestNumberFound = numb;
}
}
}
}
}
string toCreate = "alarm";
if (++highestNumberFound > 0)
{
toCreate += highestNumberFound.ToString();
}
store.CreateFile(toCreate);
}
不漂亮,但它应该有效。
我非常怀疑创建具有不同名称的空文件并不是实现您尝试做的任何事情的最佳方式。