我现在是这么一个问题。比如说D:\ee.csv文件已经手动打开的前提下我运行到下面的语句
FileStream fw = new FileStream("D:\ee.csv", FileMode.Open, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Unicode);
就会出错,说别的用户正在使用,我没有这个权限。
那我想在这个语句执行之前,先判断下这个文件是不是已经打开了。这个判断语句用那个函数。
那位高手指点下。
FileStream fw = new FileStream("D:\ee.csv", FileMode.Open, System.IO.FileAccess.Write);
StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Unicode);
就会出错,说别的用户正在使用,我没有这个权限。
那我想在这个语句执行之前,先判断下这个文件是不是已经打开了。这个判断语句用那个函数。
那位高手指点下。
解决方案 »
- 小弟新手,求大神指教C#连接oracle数据库问题
- NHibernate如何设置Where条件而不用更改代码?
- 我这里有套软件,如何知道服务器的WebServices地址呢?
- 能否在groupbox的标题栏中放一个checkbox呢?
- 51Crack 进来接分
- 请问:c#如何判断当前文件是否是打开状态
- 救救我 --急切解决:web service 调用oracle的问题(解决后马上揭帖,进来发来者有分哦)
- 怎么样绑定DataGrid.怎么样跳转页面.怎么样弹出msgBox提示窗口/
- 我用的数据库是sqlserver2005,下面是我的存储过程代码,老是说我的临时表ID无效,请问是怎么回事呢?
- 請各位大俠幫助我解決思想問題
- 未处理IndexOutOfRangeException,索引超出数组界限
- 下面这个错误怎么解决?是什么原因造成的?
try
{
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
fs.Close();}
先尝试对文件名更名,若不成功,则说明有文件被独占
有个API是查文件是否被用的,一时忘了,你去查查呀!
StreamWriter sw = new StreamWriter(fw, System.Text.Encoding.Unicode);
FileMode.Open只是Open不能写入
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
// Ensure that the file is readonly.
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly);
//Create the file.
using (FileStream fs = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read))
{
if (fs.CanWrite)
{
Console.WriteLine("The stream for file {0} is writable.", path);
}
else
{
Console.WriteLine("The stream for file {0} is not writable.", path);
}
}
}
}
Top
using System.Runtime.InteropServices;[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);[DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);public const int OF_READWRITE = 2;
public const int OF_SHARE_DENY_NONE = 0x40;
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
private void button1_Click(object sender, EventArgs e)
{
string vFileName = @"c:\temp\temp.bmp";
if (!File.Exists(vFileName))
{
MessageBox.Show("文件都不存在,你就不要拿来耍了");
return;
}
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{
MessageBox.Show("文件被占用!");
return;
}
CloseHandle(vHandle);
MessageBox.Show("没有被占用!");
}
改为
fs2 = new FileStream(("D:\ee.csv", FileMode.Open, FileAccess.Write, FileShare.ReadWrite);