VB.NET
Dim fileName As String = "C:\lock.txt"
Dim fs As New System.IO.FileStream(fileName, _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read, _
System.IO.FileShare.None)
Dim bs(fs.Length - 1) As Byte
fs.Read(bs, 0, bs.Length)
fs.Close()
C#string fileName = @"C:\lock.txt";
System.IO.FileStream fs = new System.IO.FileStream(
fileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.None);
byte[] bs = new byte[fs.Length];
fs.Read(bs, 0, bs.Length);
fs.Close();
ファイルをロックしない方法VB.NET
Dim fileName As String = "C:\lock.txt"
Dim fs As New System.IO.FileStream(fileName, _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read, _
System.IO.FileShare.ReadWrite)
Dim enc As System.Text.Encoding = _
System.Text.Encoding.GetEncoding("shift_jis")
Dim sr As New System.IO.StreamReader(fs, enc)
Dim s As String = sr.ReadToEnd()
sr.Close()
C#string fileName = @"C:\lock.txt";
System.IO.FileStream fs = new System.IO.FileStream(
fileName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite);
System.Text.Encoding enc =
System.Text.Encoding.GetEncoding("shift_jis");
System.IO.StreamReader sr = new System.IO.StreamReader(fs, enc);
string s = sr.ReadToEnd();
sr.Close();
ファイル内の一部だけにロックをかける方法VB.NET
Private testStream As New System.IO.FileStream("C:\lock.txt", _
System.IO.FileMode.Open, _
System.IO.FileAccess.Read, _
System.IO.FileShare.ReadWrite, _
10)
Private Sub btn1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn1.Click
Try
testStream.Lock(10, 20)
MessageBox.Show("Lock完了")
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn2.Click
Try
testStream.Unlock(10, 20)
MessageBox.Show("Unlock完了")
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
Try
testStream.Seek(0, System.IO.SeekOrigin.Begin)
MessageBox.Show(testStream.ReadByte().ToString())
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button4.Click
Try
testStream.Seek(29, System.IO.SeekOrigin.Begin)
MessageBox.Show(testStream.ReadByte().ToString())
Catch ex As System.IO.IOException
MessageBox.Show(ex.Message)
End Try
End Sub
Protected Overrides Sub OnFormClosed( _
ByVal e As System.Windows.Forms.FormClosedEventArgs)
testStream.Close()
MyBase.OnFormClosed(e)
End Sub
C#private System.IO.FileStream testStream = new System.IO.FileStream(
@"C:\test.txt",
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.ReadWrite,
10);
private void btn1_Click(object sender, EventArgs e)
{
try
{
testStream.Lock(10, 20);
MessageBox.Show("Lock完了");
}
catch (System.IO.IOException ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn2_Click(object sender, EventArgs e)
{
try
{
testStream.Unlock(10, 20);
MessageBox.Show("Unlock完了");
}
catch (System.IO.IOException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button3_Click(object sender, EventArgs e)
{
try
{
testStream.Seek(0, System.IO.SeekOrigin.Begin);
MessageBox.Show(testStream.ReadByte().ToString());
}
catch (System.IO.IOException ex)
{
MessageBox.Show(ex.Message);
}
}
private void Button4_Click(object sender, EventArgs e)
{
try
{
testStream.Seek(29, System.IO.SeekOrigin.Begin);
MessageBox.Show(testStream.ReadByte().ToString());
}
catch (System.IO.IOException ex)
{
MessageBox.Show(ex.Message);
}
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
testStream.Close();
base.OnFormClosed(e);
}