フォルダーをサーバー内の別のフォルダーに移動したいと思います。ローカルでは、私のコードは正常に機能します。しかし、ライブホストでは、うまくいきません。権限に関連していますか?
コードサンプル
string from = Server.MapPath(MainRoot + values[1].ToString());
string to = Server.MapPath(MainRoot + newFolderPath);
Directory.Move(from, to);
サーバーでデバッグできない場合は、コードにいくつかの検証を追加して、何が起こっているかを確認してください。次のようにします。
try
{
string from = Server.MapPath(MainRoot + values[1].ToString());
string to = Server.MapPath(MainRoot + newFolderPath);
if(!Directory.Exists(from) || !Directory.Exists(to))
{
Throw new Exception("One of the directories doesn't exist");
}
Directory.Move(from, to);
}
Catch(Exception ex)
{
File.WriteAllText("Error.txt", ex.Message);
}
実行後、Error.txt をチェックして何が起こったかを確認します。ディレクトリの 1 つが存在しない場合は例外がスローされ、権限に対して IO 操作を実行できなかった場合も例外がスローされます。ログを確認するだけです。
編集:
例外が見つかったので、実行時にディレクトリを作成します。
if(!Directory.Exists(from))
{
Directory.Create(from);
}
if(!Directory.Exists(to))
{
Directory.Create(to);
}