システム。IO.IOException:ファイルを使用する別のプロセス
-
06-07-2019 - |
質問
できないかなぁ、とおもってこのコードのような些細なもの、できなか見れが問題なのであります。私の機能、インターネットはモバイルというもの。開くファイルにコピーコンテンツの置換え文字列内にコピーして、元のファイル(簡易検索と置換中のテキストファイルします。あまり気にしなかった知っていると私は追加ラインのオリジナルファイルで、僕のコピーを作成し、ファイル(ファイルです。温度コピーまたはバックアップ(ファイルです。温度)を削除しますオリジナルファイル(file)、コピー、ファイルです。温度へのファイルです。または、例外なの削除を行ファイルです。こちらはサンプルコード:
private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
Boolean result = false;
FileStream fs = new FileStream(file.FullName + ".tmp", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
StreamReader streamreader = file.OpenText();
String originalPath = file.FullName;
string input = streamreader.ReadToEnd();
Console.WriteLine("input : {0}", input);
String tempString = input.Replace(extractedMethod, modifiedMethod);
Console.WriteLine("replaced String {0}", tempString);
try
{
sw.Write(tempString);
sw.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
streamreader.Close();
streamreader.Dispose();
File.Copy(originalPath, originalPath + ".old", true);
FileInfo newFile = new FileInfo(originalPath + ".tmp");
File.Delete(originalPath);
File.Copy(fs., originalPath, true);
result = true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return result;
}`
に関連する例外
System.IO.IOException: The process cannot access the file 'E:\mypath\myFile.cs' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(String path)
at callingMethod.modifyFile(FileInfo file, String extractedMethod, String modifiedMethod)
通常、これらの誤りから閉じてファイルの流れが私のこと。思いを忘れた重要な一歩で出ることができます。ありがとうござい皆さまのご協力のもと、
解決
音のように外部プロセス(AV?) はロックですが、できない問題を回避するためには何でしょう?
private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
try
{
string contents = File.ReadAllText(file.FullName);
Console.WriteLine("input : {0}", contents);
contents = contents.Replace(extractedMethod, modifiedMethod);
Console.WriteLine("replaced String {0}", contents);
File.WriteAllText(file.FullName, contents);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
他のヒント
を実現するのはちょっと遅めのものよりかない。また類似の問題です。使用した XMLWriter
め、その後更新のXMLファイルを果たしたのと同じ。この清潔なソリューションのための本:
の XMLWriter
使用原 FileStream
アクセスし、修正されたファイルです。問題はご利用いただきます XMLWriter.Close()
方法は、基本となるストリームな閉じると、ロックのファイルです。必要なものはインスタンスを生成する自 XMLWriter
設定を指定する必要のあることを基本となるストリームを閉鎖。
例:
XMLWriterSettings settings = new Settings();
settings.CloseOutput = true;
XMLWriter writer = new XMLWriter(filepath, settings);
希望です。
このコード作品のケージプレートもご提供してきました。い火 Sysinternalsロexplorer だが、ファイルが開きます。であることができます。
たところ、うまくいったのです。
こちらは自試験のコードです。試運転。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FileInfo f = new FileInfo(args[0]);
bool result = modifyFile(f, args[1],args[2]);
}
private static bool modifyFile(FileInfo file, string extractedMethod, string modifiedMethod)
{
Boolean result = false;
FileStream fs = new FileStream(file.FullName + ".tmp", FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
StreamReader streamreader = file.OpenText();
String originalPath = file.FullName;
string input = streamreader.ReadToEnd();
Console.WriteLine("input : {0}", input);
String tempString = input.Replace(extractedMethod, modifiedMethod);
Console.WriteLine("replaced String {0}", tempString);
try
{
sw.Write(tempString);
sw.Flush();
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
streamreader.Close();
streamreader.Dispose();
File.Copy(originalPath, originalPath + ".old", true);
FileInfo newFile = new FileInfo(originalPath + ".tmp");
File.Delete(originalPath);
File.Copy(originalPath + ".tmp", originalPath, true);
result = true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return result;
}
}
}
C:\testarea>ConsoleApplication1.exe file.txt padding testing
input : <style type="text/css">
<!--
#mytable {
border-collapse: collapse;
width: 300px;
}
#mytable th,
#mytable td
{
border: 1px solid #000;
padding: 3px;
}
#mytable tr.highlight {
background-color: #eee;
}
//-->
</style>
replaced String <style type="text/css">
<!--
#mytable {
border-collapse: collapse;
width: 300px;
}
#mytable th,
#mytable td
{
border: 1px solid #000;
testing: 3px;
}
#mytable tr.highlight {
background-color: #eee;
}
//-->
</style>
来はこのエラーとは何でもweb上に設ん、ようと思ってもなっているこ例外ということをソースとデスティネーションパスのファイルをコピーコマンドは同じです。かかったとのことつでもありに追加コードのどこかに例外をスローする場合、ipパスポイントと同じファイルです。
幸運を祈っています。
ありませ実時間ウイルス対策スキャナに位置する?もしそうならば、もう時効で見であればアクセスするファイルしようとしています。(クリス案使用Sysinternalsロexplorerは良い)してご利用いただけます。
してみてください:でない場合は、ファイルな存在で作成し、その書き込みます。が既に存在していない問題でオープンで書き込み:
using (FileStream fs= new FileStream(@"File.txt",FileMode.Create,FileAccess.ReadWrite))
{
fs.close();
}
using (StreamWriter sw = new StreamWriter(@"File.txt"))
{
sw.WriteLine("bla bla bla");
sw.Close();
}
作成後、作成したファイルだけの力はプレーンテキストとしてリソースを解放します:
//FSm is stream for creating file on a path//
System.IO.FileStream FS = new System.IO.FileStream(path + fname,
System.IO.FileMode.Create);
pro.CopyTo(FS);
FS.Dispose();
System.Drawing.Image FileUploadPhoto = System.Drawing.Image.FromFile(location1);
FileUploadPhoto.Save(location2);
FileUploadPhoto.Dispose();