小编Yod*_*age的帖子

C#创建目录并设置权限

我试图使用下面的代码允许所有用户能够修改文件夹:

class Program
{
    private const string FileName = "test.txt";
    private static readonly string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\TEST\\" + FileName;
    private static readonly string UserName = Environment.UserDomainName;

    static void Main()
    {
        DirectorySecurity securityRules = new DirectorySecurity();

        string dirPath = Path.GetDirectoryName(FilePath);

        securityRules.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.Modify, AccessControlType.Allow));

        if (dirPath == null) throw new InvalidOperationException("Failure to save local security settings");

        if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath, securityRules);

        File.WriteAllText(FilePath, "Test test!");
    }
}
Run Code Online (Sandbox Code Playgroud)

运行代码后,"用户"将添加到文件夹中,但不会分配任何权限.所有的读,写,执行等都不会被选中.除了特殊权限被选中.

如何为所有用户添加包含所有修改的文件夹?

c# windows

5
推荐指数
1
解决办法
8347
查看次数

if else和return之间有所不同

我的第一个片段:

public bool isSmall(int x)
{
    if (x == 0)
    {
        return true;
    }
    else
    {
        if (x < Smallest)
        {
            return true;
        }
        else
        {
            return false;
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

我的第二个片段:

public bool isSmall(int x)
{
    if (x == 0)
    {
        return true;
    }

    if (x < Smallest)
    {
        return true;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我想要的if (x == 0)只是返回true,如果不做另一个比较,if (x < Smallest)如果true返回true,否则返回false.我知道我的拳头和第二个代码做同样的事情,但我想知道哪种方式写它更好,更亲切,更安全?他们有什么真正的区别吗?或者只是两种方式来写它?

c# boolean-expression

3
推荐指数
1
解决办法
93
查看次数

如何添加服务提供者

对于组件(@Component),我可以使用“providers: []”来添加注入令牌:

export const WINDOW_TOKEN = new InjectionToken<Window>('Window object');

@Component({
  providers: [
    { provide: WINDOW_TOKEN, useValue: window }
  ]
})
export class ExampleClass {
  constructor(@Inject(WINDOW_TOKEN) private windowObj: Window) {}
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我不必为这个简单的注入创建另一个服务。另外,这WINDOW_TOKEN将仅在ExampleClass. 更重要的是,我的测试用例可以window.open()通过创建一个间谍来测试windowObj

但是,我如何为服务(@Injectable)做类似的事情?

@Injectable({
  provideIn: 'root'
})
export class ExampleService {
  constructor() {}
}
Run Code Online (Sandbox Code Playgroud)

因此,对于这段代码,我如何创建一个@Inject并仅在 this 中提供ExampleService,而不是创建另一个@Injectable并在根或模块中提供。

angular

3
推荐指数
1
解决办法
5855
查看次数

标签 统计

c# ×2

angular ×1

boolean-expression ×1

windows ×1