6

アプリに同梱するテキスト ファイルに奇妙な問題があります。

このファイルには多数のサイトが含まれており、プログラムが開始されるとサイトが配列にロードされます。

Windows 7 では、アプリを起動してもエラーは発生しません。ただし、XPでは、c:\Document and setting\I\Application Data\fourmlinks.txt file not found. 奇妙な部分は、コンテンツを含むテキストファイルを作成し、それをアプリケーションフォルダー内に配置したことです。

これが私のコードで呼び出す方法です:

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";

私の問題は、アプリが必要として使用している基本的なデータを保持しているため、新しいファイルを作成できないことです。

最初の起動後、ユーザーは必要に応じてファイルを編集できます。

原因はわかりませんが、これは Windows XP でのみ発生します。

どうすればこの問題を解決できますか?

編集


keyboardP は、実行中のウィンドウを確認し、それによってパスを変更することをお勧めします。だから私はこのコードを思いついた:

 System.OperatingSystem osInfo = System.Environment.OSVersion;
            if (osInfo.Platform == PlatformID.Win32NT)
                path = Environment.SpecialFolder.LocalApplicationData + "\\fourmlinks.txt";
            else
                path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";

Windows 7 でも false になる必要があるときに true になるという問題。XP または Windows 7 で別の方法で確実に実行する方法はありますか?


編集2


オペレーティング システムのチェックを使用して、Windows 7 または Windows XP であることを確認できるようになりました。そのため、コードは Windows 7 で再び find を実行しますが、Windows XP では別のエラー メッセージが表示されます。

ここに画像の説明を入力

プログラムに追加したパスが、エラーが要求していると言っているパスになる方法が本当にわかりません。

4

2 に答える 2

3

ユーザーが実行している現在のオペレーティング システムを検出するにはSystem.OperatingSystem、次の Windows バージョンに対応する 3 つのコンポーネントで構成されるものを使用できます。

+-----------------------------------------------------------------------------------------------------------------------------------------+
|           |   Windows    |   Windows    |   Windows    |Windows NT| Windows | Windows | Windows | Windows | Windows | Windows | Windows |
|           |     95       |      98      |     Me       |    4.0   |  2000   |   XP    |  2003   |  Vista  |  2008   |    7    | 2008 R2 |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|PlatformID | Win32Windows | Win32Windows | Win32Windows | Win32NT  | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|Major      |              |              |              |          |         |         |         |         |         |         |         |
| version   |      4       |      4       |      4       |    4     |    5    |    5    |    5    |    6    |    6    |    6    |    6    |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|Minor      |              |              |              |          |         |         |         |         |         |         |         |
| version   |      0       |     10       |     90       |    0     |    0    |    1    |    2    |    0    |    0    |    1    |    1    |
+-----------------------------------------------------------------------------------------------------------------------------------------+

はorであるため、MajorMinorのバージョンを知っていれば十分です。PlatFormIDWin32WindowsWin32NT

次の例はOperatingSystem、ユーザーの現在のオペレーティング システムを検出するために使用する方法を示しています。

int getOSArchitecture() 
{
    //Only required if you would like to show the user's processor architecture (32-bit / 64-bit)
    string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
    return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? 32 : 64);
}

string getOSInfo()
{
    //Get Operating system information.
    OperatingSystem os = Environment.OSVersion;
    //Get version information about the os.
    Version vs = os.Version;

    //Variable to hold our return value
    string operatingSystem = "";

    if (os.Platform == PlatformID.Win32Windows)
    {
        //This is a pre-NT version of Windows
        switch (vs.Minor)
        {
            case 0:
                operatingSystem = "95";
                break;
            case 10:
                if (vs.Revision.ToString() == "2222A")
                    operatingSystem = "98SE";
                else
                    operatingSystem = "98";
                break;
            case 90:
                operatingSystem = "Me";
                break;
            default:
                break;
        }
    }
    else if (os.Platform == PlatformID.Win32NT)
    {
        switch (vs.Major)
        {
            case 3:
                operatingSystem = "NT 3.51";
                break;
            case 4:
                operatingSystem = "NT 4.0";
                break;
            case 5:
                if (vs.Minor == 0)
                    operatingSystem = "2000";
                else
                    operatingSystem = "XP";
                break;
            case 6:
                if (vs.Minor == 0)
                    operatingSystem = "Vista";
                else
                    operatingSystem = "7";
                break;
            default:
                break;
        }
    }
    //Make sure we actually got something in our OS check
    //We don't want to just return " Service Pack 2" or " 32-bit"
    //That information is useless without the OS version.
    if (operatingSystem != "")
    {
        //Got something.  Let's prepend "Windows" and get more info.
        operatingSystem = "Windows " + operatingSystem;
        //See if there's a service pack installed.
        if (os.ServicePack != "")
        {
            //Append it to the OS name.  i.e. "Windows XP Service Pack 3"
            operatingSystem += " " + os.ServicePack;
        }
        //Append the OS architecture.  i.e. "Windows XP Service Pack 3 32-bit"
        operatingSystem += " " + getOSArchitecture().ToString() + "-bit"; //Remove this if you do not want to show the processor architecture
    }
    //Return the information we've gathered.
    return operatingSystem;
}

上記の例を使用すると、たとえばWindows XP Service Pack 3、ユーザーが Windows XP Service Pack 3 を実行している場合に、getOSInfo();


Windows 7 Service Pack 1 32 ビットの実行


ありがとう
、これが役立つことを願っています:)

于 2012-10-22T21:48:12.120 に答える
1

XP では、 を使用してみてくださいEnvironment.SpecialFolder.LocalApplicationData

コメントから編集

path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
System.OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Platform == PlatformID.Win32NT)
{
   if(osInfo.Version.Major == 5 && osInfo.Version.Minor != 0) 
   {
      //running XP
      path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\fourmlinks.txt"; 
   } 
}
于 2012-10-22T21:03:23.060 に答える