相关疑难解决方法(0)

在MS Excel中打开文件时无法写入文件

我正在给文本文件写一些数据.我正在使用此代码:

  using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
  {
      using (TextWriter tw = new StreamWriter(fs))
      {
      tw.WriteLine("sample_data");
      }
   }
Run Code Online (Sandbox Code Playgroud)

当记事本打开文件时,我的应用程序可以写入其中.当MS Excel打开此文件时,我收到以下错误:进程无法访问文件myfile.csv,因为它正由另一个进程使用.什么可能导致这种情况,我该如何解决这个问题?

.net c# io filestream

4
推荐指数
1
解决办法
1896
查看次数

关于抛出和异常冒泡

我的问题是关于抛出和异常冒泡.我正在四处搜索文件锁定和C#,我试着弄乱别人的代码,这让我怀疑我对抛出和异常冒泡有多了解.

是线程的链接.

public class FileManager
{
private string _fileName;
private int _numberOfTries;

private int _timeIntervalBetweenTries;

private FileStream GetStream(FileAccess fileAccess)
{
    var tries = 0;
    while (true)
    {
        try
        {
            return File.Open(_fileName, FileMode.Open, fileAccess, Fileshare.None); 
        }
        catch (IOException e)
        {
            if (!IsFileLocked(e))
                throw;
            if (++tries > _numberOfTries)
                throw new MyCustomException("The file is locked too long: " + e.Message, e);
            Thread.Sleep(_timeIntervalBetweenTries);
        }
    }
}

private static bool IsFileLocked(IOException exception)
{
    int errorCode = Marshal.GetHRForException(exception) & ((1 << 16) - 1);
    return …
Run Code Online (Sandbox Code Playgroud)

c# exception-handling

3
推荐指数
1
解决办法
5782
查看次数

.NET中的IMAPI2 MsftFileSystemImage在创建ISO时不释放文件

我成功地创建了iso映像,但是在调用此Create方法返回后,我尝试删除rootFolderPath中的文件时出现'文件正在使用'IO错误.我错过了Marshal.ReleaseComObject调用吗?

谢谢,

CW

/// <summary>
/// Create iso image from rootFolderPath and write to isoImageFilePath. Does not include the actual rootFolder itself
/// </summary>
public void Create()
{
    IFileSystemImage ifsi = new MsftFileSystemImage();
    try
    {
        ifsi.ChooseImageDefaultsForMediaType(IMAPI_MEDIA_PHYSICAL_TYPE.IMAPI_MEDIA_TYPE_DISK);
        ifsi.FileSystemsToCreate =
                FsiFileSystems.FsiFileSystemJoliet | FsiFileSystems.FsiFileSystemISO9660;
        ifsi.VolumeName = this.volumeName;
        ifsi.Root.AddTree(rootFolderPath, false);//use a valid folder
        //this will implement the Write method for the formatter
        IStream imagestream = ifsi.CreateResultImage().ImageStream;
        if (imagestream != null)
        {
            System.Runtime.InteropServices.ComTypes.STATSTG stat;
            imagestream.Stat(out stat, 0x01);
            IStream newStream;
            if (0 == SHCreateStreamOnFile(isoImageFilepath, 0x00001001, out …
Run Code Online (Sandbox Code Playgroud)

c# interop imapi

3
推荐指数
1
解决办法
2664
查看次数

测试文件的可访问性

避免像

(1)进程无法访问该文件,因为它被另一个进程使用

在进行任何进一步处理之前,我使用以下方法测试文件的可访问性.

private bool CheckIfFileBeingUsed(string FilePath)
    {
        FileStream Fs = null;
        try
        {
            Fs = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None);
            Fs.Close();
        }
        catch (Exception)
        {
            return true; //Error File is being used
        }
        return false; //File is not being used.
    }
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我有任何Windows API或其他解决方案来测试文件可访问性而不是上面的File.Open方法吗?

c# file

1
推荐指数
1
解决办法
1724
查看次数

标签 统计

c# ×4

.net ×1

exception-handling ×1

file ×1

filestream ×1

imapi ×1

interop ×1

io ×1