私の.NET 2.0アプリケーションでは、ファイルを作成してディレクトリに書き込むための十分な権限があるかどうかを確認する必要があります。この目的のために、私はファイルを作成しそれにシングルバイトを書き込もうとする次の関数を持っています。その後、自分自身を削除してパーミッションが存在することをテストします。
私がチェックする最良の方法は、実際に試してみて、発生した例外をすべて検出することでした。私は一般的な例外のキャッチについては特にうんざりしていません、それでこれを行うより良いまたはおそらくより受け入れられた方法がありますか?
private const string TEMP_FILE = "\\tempFile.tmp";
/// <summary>
/// Checks the ability to create and write to a file in the supplied directory.
/// </summary>
/// <param name="directory">String representing the directory path to check.</param>
/// <returns>True if successful; otherwise false.</returns>
private static bool CheckDirectoryAccess(string directory)
{
    bool success = false;
    string fullPath = directory + TEMP_FILE;
    if (Directory.Exists(directory))
    {
        try
        {
            using (FileStream fs = new FileStream(fullPath, FileMode.CreateNew, 
                                                            FileAccess.Write))
            {
                fs.WriteByte(0xff);
            }
            if (File.Exists(fullPath))
            {
                File.Delete(fullPath);
                success = true;
            }
        }
        catch (Exception)
        {
            success = false;
        }
    }
                    
                
                
                                    
string fullPath = directory + TEMP_FILE;fullPathを取得するために文字列を連結する代わりにPath.Combineメソッドを使用してください。Path.Combine(directory, TEMP_FILE)- nomail