获取%AppData%的路径返回错误的值(Getting path to %AppData% returns wrong value)
我有一个dotnet核心应用程序,我想获得本地%Appdata%目录的路径。 dotnet核心应用程序是从之前安装的Windows服务启动的。 我使用了这个问题所描述的方法,但它们都回到了错误的道路上。
代替
C:\ Users \用户MYUSER \应用程序数据\漫游\ MyApplication的\ file.txt的
我明白了
C:\ WINDOWS \ ServiceProfiles \本地服务\应用程序数据\漫游\ MyApplication的\ file.txt的
1. Environment.SpecialFolder.ApplicationData
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApplication", "file.txt")
2. Environment.GetEnvironmentVariable(“APPDATA”)
Path.Combine(Environment.GetEnvironmentVariable("APPDATA"), "MyApplication", "file.txt")
3. Environment.ExpandEnvironmentVariables(“%AppData%”)
Path.Combine(Environment.ExpandEnvironmentVariables("%AppData%"), "MyApplication", "file.txt")
所有方法都遗憾地回到了错误的道路上。 在使用Windows服务启动应用程序时,如何获得正确的%AppData%路径?
I have a dotnet core application and I want to get the path to the local %Appdata% directory. The dotnet core application is started from a Windows Service installed previously. I used the methods decribed under this question but they all return the wrong path.
Instead of
C:\Users\MyUser\AppData\Roaming\MyApplication\file.txt
I get
C:\WINDOWS\ServiceProfiles\LocalService\AppData\Roaming\MyApplication\file.txt
1. Environment.SpecialFolder.ApplicationData
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApplication", "file.txt")
2. Environment.GetEnvironmentVariable("APPDATA")
Path.Combine(Environment.GetEnvironmentVariable("APPDATA"), "MyApplication", "file.txt")
3. Environment.ExpandEnvironmentVariables("%AppData%")
Path.Combine(Environment.ExpandEnvironmentVariables("%AppData%"), "MyApplication", "file.txt")
All methods sadly returned the wrong path. How can I get the right %AppData% path, while the application is started with a Windows Service?
原文:https://stackoverflow.com/questions/50561685
最满意答案
可能有一种
cumcount
方法,但每组中的第一个元素是该组中的第0个元素,而cumcount
为每个组中的元素编号。 所以:In [19]: df.loc[df.groupby('series1').cumcount() == 0, 'series2'] = np.nan In [20]: df Out[20]: series1 series2 0 A NaN 1 A 1.0 2 A 10.0 3 A 99.0 4 B NaN 5 B 9.0 6 B 0.0 7 C NaN 8 C 20.0 9 C 10.0 10 C 10.0
There's probably a slicker way to do this, but the first element in each group is the 0th element in that group, and
cumcount
numbers the elements within each group. So:In [19]: df.loc[df.groupby('series1').cumcount() == 0, 'series2'] = np.nan In [20]: df Out[20]: series1 series2 0 A NaN 1 A 1.0 2 A 10.0 3 A 99.0 4 B NaN 5 B 9.0 6 B 0.0 7 C NaN 8 C 20.0 9 C 10.0 10 C 10.0
相关问答
更多-
NaN一直被用作丢失的占位符 ,当用“缺失”替换部分字符串时,它只能意味着整个条目被泄露。 我听说这叫做NaN污染(或类似的,看看我能不能找到一些参考资料),因为如果NaN触及数据就会受到损害。 那就是说,情况并非总是如此: In [11]: s = pd.Series([1, 2, np.nan, 4]) In [12]: s.sum() Out[12]: 7.0 In [13]: s.sum(skipna=False) Out[13]: nan 在某些语言中,您会看到skipna = False作 ...
-
c = np.where(np.isnan(b), 3, b) c = np.where(np.isnan(b), 3, b)
-
可能有一种cumcount方法,但每组中的第一个元素是该组中的第0个元素,而cumcount为每个组中的元素编号。 所以: In [19]: df.loc[df.groupby('series1').cumcount() == 0, 'series2'] = np.nan In [20]: df Out[20]: series1 series2 0 A NaN 1 A 1.0 2 A 10.0 3 A 9 ...
-
你为什么要比较? 不同的值可能不一定是同一个实例。 对于整数和浮点数都是如此。 (CPython对小整数的怪癖是我所知道的唯一例外,并且严格来说是一个实现细节。) >>> import math >>> float('nan') is math.nan False 您可以使用np.isnan来检查nan或内置的math.isnan ,或检查该值是否等于它自己。 >>> np.isnan([12.6,np.nan,0.5,4.6]) array([False, True, False, False], d ...
-
这是由于解释者评估表达式的方式and / or : or表达式: 如果第一个操作数为True或等于True ,则不计算第二个操作数,并返回第一个操作数的值。 如果第一个操作数是False或等于False ,则会计算并返回第二个操作数 例子: True or np.nan : bool(True)为True ,因此返回True np.nan or True : bool(np.nan)是True ,因此返回np.nan False or np.nan : bool(False)为False ,因此返回np.n ...
-
NaN具有不等于自身的属性,因此应该使用np.isnan来测试NaN值,这里np.isnan(np.nan)将产生True : In[5]: np.nan == np.nan Out[5]: False In[6]: np.nan != np.nan Out[6]: True In[7]: np.isnan(np.nan) Out[7]: True 正如@ŁukaszRogalski指出的那样,这里也可以工作: In [10]: np.nan is np.nan Out[10]: True ...
-
如何将布尔数据帧转换为1和np.NaN的数据帧?(how to convert dataframe of booleans to dataframe of 1 and np.NaN?)[2022-03-31]
applymap()可用于将函数应用于dataframe每个元素 In [1]: df = DataFrame([[True, True, False],[False, False, True]]).T In [2]: df Out[2]: 0 1 0 True False 1 True False 2 False True In [3]: df.applymap(lambda x: 1 if x else np.nan) Out[3]: 0 1 0 ... -
对于pandas DataFrame中的空值,请使用None而不是np.nan(Use None instead of np.nan for null values in pandas DataFrame)[2023-07-03]
使用pd.DataFrame.where 条件满足时使用df值,否则使用None df.where(df.notnull(), None) Use pd.DataFrame.where Uses df value when condition is met, otherwise uses None df.where(df.notnull(), None) -
更新以匹配更新的Q. >>> np.array([[np.nan, np.nan], *[(x, x+2) for x in range(10) if x%2 ==0]]) array([[nan, nan], [ 0., 2.], [ 2., 4.], [ 4., 6.], [ 6., 8.], [ 8., 10.]]) 更新结束。 我想你要做的是 np.array([[np.nan, np.nan], *5*[[1, 2]] ...
-
你可以通过fillna()得到min()的结果: df2['data'].fillna(df2[['a', 'b']].min(axis=1), inplace=True) You can just fillna() the results from min(): df2['data'].fillna(df2[['a', 'b']].min(axis=1), inplace=True)